-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdream_bootblob.c
More file actions
223 lines (210 loc) · 6.7 KB
/
Copy pathdream_bootblob.c
File metadata and controls
223 lines (210 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Dream DM820/DM7080 boot container and bounded bank A writer.
* GPL-2.0-or-later. Container layout verified against mkbootblob a461d519
* and a byte-for-byte capture of a DM820 boot image. No external helpers.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "dream_bootblob.h"
static uint32_t get_le32(const unsigned char *p)
{
return (uint32_t)p[0] | (uint32_t)p[1] << 8 |
(uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
}
int dream_bootblob_parse(const unsigned char *header, size_t header_size,
struct dream_bootblob_parts *parts)
{
uint32_t asize, ksize;
size_t i;
if (!header || header_size < 512 || !parts)
goto invalid;
asize = get_le32(header);
ksize = get_le32(header + 16);
/* Only the standard animation + kernel container can be restored
* losslessly through the existing flash-kernel/ofgwrite paths.
*/
if (!asize || !ksize || asize > DREAM_BANK_SIZE || ksize > DREAM_BANK_SIZE ||
asize % 4096 || ksize % 4096 || 512ULL + asize + ksize > DREAM_BANK_SIZE ||
get_le32(header + 4) != 1 || get_le32(header + 8) != 0x10000000U - asize ||
get_le32(header + 12) != 8 || get_le32(header + 20) != 1 + asize / 512 ||
get_le32(header + 24) != 0x1000 || get_le32(header + 28) != 1)
goto invalid;
for (i = 32; i < 512; ++i)
if (header[i])
goto invalid;
parts->animation_offset = 512;
parts->animation_size = asize;
parts->kernel_offset = 512 + asize;
parts->kernel_size = ksize;
parts->blob_size = 512 + asize + ksize;
return 1;
invalid:
errno = EINVAL;
return 0;
}
static int read_at(int fd, unsigned char *data, size_t size, off_t offset)
{
size_t done = 0;
while (done < size) {
size_t count = size - done;
ssize_t n;
if (count > 65536)
count = 65536;
n = pread(fd, data + done, count, offset + (off_t)done);
if (n < 0 && errno == EINTR)
continue;
if (n <= 0) {
if (!n)
errno = EIO;
return 0;
}
done += (size_t)n;
}
return 1;
}
int dream_bootblob_read_a(int fd, unsigned char **blob, size_t *size)
{
unsigned char buffer[65536] __attribute__((aligned(4096)));
unsigned char header[512];
struct dream_bootblob_parts parts;
unsigned char *data;
void *aligned = NULL;
size_t done;
int error;
*blob = NULL;
*size = 0;
if (!read_at(fd, buffer, 512, DREAM_BANK_A_OFFSET) ||
!dream_bootblob_parse(buffer, 512, &parts))
return 0;
memcpy(header, buffer, 512);
error = posix_memalign(&aligned, 4096, parts.blob_size);
if (error) { errno = error; return 0; }
data = aligned;
if (!read_at(fd, data, parts.blob_size, DREAM_BANK_A_OFFSET))
goto fail;
if (memcmp(header, data, 512)) { errno = EIO; goto fail; }
/* A second direct read rejects a bank changing during the backup. */
for (done = 0; done < parts.blob_size;) {
size_t count = parts.blob_size - done;
if (count > sizeof(buffer))
count = sizeof(buffer);
if (!read_at(fd, buffer, count, DREAM_BANK_A_OFFSET + (off_t)done))
goto fail;
if (memcmp(buffer, data + done, count)) { errno = EIO; goto fail; }
done += count;
}
*blob = data;
*size = parts.blob_size;
return 1;
fail:
free(data);
return 0;
}
static void put_le32(unsigned char *p, uint32_t value)
{
unsigned i;
for (i = 0; i < 4; ++i)
p[i] = (unsigned char)(value >> (8 * i));
}
static void entry(unsigned char *p, size_t size, unsigned sector,
uint32_t destination, unsigned type)
{
put_le32(p, (uint32_t)size);
put_le32(p + 4, sector);
put_le32(p + 8, destination);
put_le32(p + 12, type);
}
int dream_bootblob_create(const void *kernel, size_t kernel_size,
const void *animation, size_t animation_size,
unsigned char **blob, size_t *blob_size)
{
size_t ksize, asize, total;
unsigned char *data;
void *aligned = NULL;
int error;
*blob = NULL;
*blob_size = 0;
if (!kernel || !animation || kernel_size < 4096 || !animation_size) {
errno = EINVAL;
return 0;
}
if (kernel_size > DREAM_BANK_SIZE || animation_size > DREAM_BANK_SIZE) {
errno = EFBIG;
return 0;
}
ksize = (kernel_size + 4095) & ~(size_t)4095;
asize = (animation_size + 4095) & ~(size_t)4095;
total = 512 + asize + ksize;
if (total > DREAM_BANK_SIZE) {
errno = EFBIG;
return 0;
}
/* Also suitable for the block device's O_DIRECT I/O. */
error = posix_memalign(&aligned, 4096, total);
if (error) {
errno = error;
return 0;
}
data = aligned;
memset(data, 0, total);
/* Sector positions are relative to the beginning of the chosen bank.
* Only payload lengths are aligned to 4 KiB; the table is 512 bytes.
*/
entry(data, asize, 1, 0x10000000U - (uint32_t)asize, 8);
entry(data + 16, ksize, 1 + (unsigned)(asize / 512), 0x1000, 1);
memcpy(data + 512, animation, animation_size);
memcpy(data + 512 + asize, kernel, kernel_size);
*blob = data;
*blob_size = total;
return 1;
}
int dream_bootblob_write_a(int fd, const unsigned char *blob, size_t size,
void (*progress)(int))
{
unsigned char verify[65536] __attribute__((aligned(4096)));
size_t done = 0;
if (!blob || size < 512 || size > DREAM_BANK_SIZE || size % 512) {
errno = EINVAL;
return 0;
}
while (done < size) {
size_t count = size - done;
ssize_t n;
if (count > sizeof(verify))
count = sizeof(verify);
n = pwrite(fd, blob + done, count, DREAM_BANK_A_OFFSET + (off_t)done);
if (n < 0 && errno == EINTR)
continue;
if (n <= 0) {
if (!n)
errno = EIO;
return 0;
}
done += (size_t)n;
if (progress)
progress((int)(done * 50 / size));
}
if (fsync(fd))
return 0;
done = 0;
while (done < size) {
size_t count = size - done;
ssize_t n;
if (count > sizeof(verify))
count = sizeof(verify);
n = pread(fd, verify, count, DREAM_BANK_A_OFFSET + (off_t)done);
if (n < 0 && errno == EINTR)
continue;
if (n <= 0 || (n > 0 && memcmp(verify, blob + done, (size_t)n))) {
if (n >= 0)
errno = EIO;
return 0;
}
done += (size_t)n;
if (progress)
progress(50 + (int)(done * 50 / size));
}
return 1;
}