diff options
Diffstat (limited to 'lib/x1000-installer/src')
-rw-r--r-- | lib/x1000-installer/src/xf_error.c | 42 | ||||
-rw-r--r-- | lib/x1000-installer/src/xf_flashmap.c | 313 | ||||
-rw-r--r-- | lib/x1000-installer/src/xf_nandio.c | 295 | ||||
-rw-r--r-- | lib/x1000-installer/src/xf_package.c | 264 | ||||
-rw-r--r-- | lib/x1000-installer/src/xf_stream.c | 214 | ||||
-rw-r--r-- | lib/x1000-installer/src/xf_update.c | 149 |
6 files changed, 1277 insertions, 0 deletions
diff --git a/lib/x1000-installer/src/xf_error.c b/lib/x1000-installer/src/xf_error.c new file mode 100644 index 0000000000..3d4b342a92 --- /dev/null +++ b/lib/x1000-installer/src/xf_error.c | |||
@@ -0,0 +1,42 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2021 Aidan MacDonald | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "xf_error.h" | ||
23 | |||
24 | const char* xf_strerror(int err) | ||
25 | { | ||
26 | switch(err) { | ||
27 | case XF_E_SUCCESS: return "Success"; | ||
28 | case XF_E_IO: return "I/O error"; | ||
29 | case XF_E_LINE_TOO_LONG: return "Line too long"; | ||
30 | case XF_E_FILENAME_TOO_LONG: return "Filename too long"; | ||
31 | case XF_E_INT_OVERFLOW: return "Numeric overflow"; | ||
32 | case XF_E_BUF_OVERFLOW: return "Buffer overflowed"; | ||
33 | case XF_E_SYNTAX_ERROR: return "Syntax error"; | ||
34 | case XF_E_INVALID_PARAMETER: return "Invalid parameter"; | ||
35 | case XF_E_NAND: return "NAND flash error"; | ||
36 | case XF_E_OUT_OF_MEMORY: return "Out of memory"; | ||
37 | case XF_E_OUT_OF_RANGE: return "Out of range"; | ||
38 | case XF_E_VERIFY_FAILED: return "Verification failed"; | ||
39 | case XF_E_CANNOT_OPEN_FILE: return "Cannot open file"; | ||
40 | default: return "Unknown error"; | ||
41 | } | ||
42 | } | ||
diff --git a/lib/x1000-installer/src/xf_flashmap.c b/lib/x1000-installer/src/xf_flashmap.c new file mode 100644 index 0000000000..75cd3c5905 --- /dev/null +++ b/lib/x1000-installer/src/xf_flashmap.c | |||
@@ -0,0 +1,313 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2021 Aidan MacDonald | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "xf_flashmap.h" | ||
23 | #include "xf_error.h" | ||
24 | #include <stdbool.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <ctype.h> | ||
27 | #include <string.h> | ||
28 | |||
29 | static int xdigit_to_int(char c) | ||
30 | { | ||
31 | if(c >= 'a' && c <= 'f') | ||
32 | return 10 + (c - 'a'); | ||
33 | if(c >= 'A' && c <= 'F') | ||
34 | return 10 + (c - 'A'); | ||
35 | if(c >= '0' && c <= '9') | ||
36 | return c - '0'; | ||
37 | return -1; | ||
38 | } | ||
39 | |||
40 | int xf_map_parseline(const char* line, struct xf_map* map) | ||
41 | { | ||
42 | enum { | ||
43 | s_name, | ||
44 | s_md5, | ||
45 | s_first_num, | ||
46 | s_file_len = s_first_num, | ||
47 | s_offset, | ||
48 | s_length, | ||
49 | s_done, | ||
50 | }; | ||
51 | |||
52 | #define skipws() do { while(*line == ' ' || *line == '\t') ++line; } while(0) | ||
53 | #define nextstate() do { ++state; length = 0; ++line; skipws(); } while(0) | ||
54 | |||
55 | int state = s_name; | ||
56 | int length = 0; | ||
57 | int digit_val; | ||
58 | uint32_t int_val; | ||
59 | uint32_t* num_ptr[3] = {&map->file_length, &map->offset, &map->length}; | ||
60 | bool has_md5 = true; | ||
61 | |||
62 | skipws(); | ||
63 | while(*line && *line != '\n') { | ||
64 | switch(state) { | ||
65 | case s_name: | ||
66 | if(*line == ' ' || *line == '\t') { | ||
67 | nextstate(); | ||
68 | continue; | ||
69 | } else if(isgraph((unsigned char)*line)) { | ||
70 | if(length == XF_MAP_NAMELEN) | ||
71 | return XF_E_FILENAME_TOO_LONG; | ||
72 | |||
73 | map->name[length++] = *line++; | ||
74 | map->name[length] = '\0'; | ||
75 | continue; | ||
76 | } else { | ||
77 | return XF_E_SYNTAX_ERROR; | ||
78 | } | ||
79 | |||
80 | case s_md5: | ||
81 | if(*line == '-') { | ||
82 | memset(map->md5, 0, 16); | ||
83 | map->file_length = 0; | ||
84 | has_md5 = false; | ||
85 | ++line; | ||
86 | } else { | ||
87 | for(int i = 0; i < 16; ++i) { | ||
88 | int_val = 0; | ||
89 | for(int j = 0; j < 2; ++j) { | ||
90 | digit_val = xdigit_to_int(*line++); | ||
91 | if(digit_val < 0) | ||
92 | return XF_E_SYNTAX_ERROR; | ||
93 | |||
94 | int_val <<= 4; | ||
95 | int_val |= digit_val; | ||
96 | } | ||
97 | |||
98 | map->md5[i] = int_val; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | if(*line == ' ' || *line == '\t') { | ||
103 | /* skip file length if md5 is not present */ | ||
104 | if(!has_md5) | ||
105 | ++state; | ||
106 | |||
107 | nextstate(); | ||
108 | continue; | ||
109 | } else { | ||
110 | return XF_E_SYNTAX_ERROR; | ||
111 | } | ||
112 | |||
113 | case s_file_len: | ||
114 | case s_offset: | ||
115 | case s_length: | ||
116 | int_val = 0; | ||
117 | |||
118 | if(*line == '0') { | ||
119 | ++line; | ||
120 | if(*line == 'x' || *line == 'X') { | ||
121 | ++line; | ||
122 | while((digit_val = xdigit_to_int(*line)) >= 0) { | ||
123 | ++line; | ||
124 | |||
125 | if(int_val > UINT32_MAX/16) | ||
126 | return XF_E_INT_OVERFLOW; | ||
127 | int_val *= 16; | ||
128 | |||
129 | if(int_val > UINT32_MAX - digit_val) | ||
130 | return XF_E_INT_OVERFLOW; | ||
131 | int_val |= digit_val; | ||
132 | } | ||
133 | } | ||
134 | } else if(*line >= '1' && *line <= '9') { | ||
135 | do { | ||
136 | if(int_val > UINT32_MAX/10) | ||
137 | return XF_E_INT_OVERFLOW; | ||
138 | int_val *= 10; | ||
139 | |||
140 | digit_val = *line++ - '0'; | ||
141 | if(int_val > UINT32_MAX - digit_val) | ||
142 | return XF_E_INT_OVERFLOW; | ||
143 | |||
144 | int_val += digit_val; | ||
145 | } while(*line >= '0' && *line <= '9'); | ||
146 | } | ||
147 | |||
148 | *num_ptr[state - s_first_num] = int_val; | ||
149 | |||
150 | if(*line == ' ' || *line == '\t') { | ||
151 | nextstate(); | ||
152 | continue; | ||
153 | } else if(state+1 == s_done && *line == '\0') { | ||
154 | /* end of input */ | ||
155 | continue; | ||
156 | } else { | ||
157 | return XF_E_SYNTAX_ERROR; | ||
158 | } | ||
159 | |||
160 | case s_done: | ||
161 | if(isspace(*line)) { | ||
162 | line++; | ||
163 | continue; /* swallow trailing spaces, carriage return, etc */ | ||
164 | } else | ||
165 | return XF_E_SYNTAX_ERROR; | ||
166 | } | ||
167 | } | ||
168 | |||
169 | #undef skipws | ||
170 | #undef nextstate | ||
171 | |||
172 | /* one last overflow check - ensure mapped range is addressable */ | ||
173 | if(map->offset > UINT32_MAX - map->length) | ||
174 | return XF_E_INT_OVERFLOW; | ||
175 | |||
176 | if(has_md5) | ||
177 | map->flags = XF_MAP_HAS_MD5 | XF_MAP_HAS_FILE_LENGTH; | ||
178 | else | ||
179 | map->flags = 0; | ||
180 | |||
181 | return XF_E_SUCCESS; | ||
182 | } | ||
183 | |||
184 | struct map_parse_args { | ||
185 | struct xf_map* map; | ||
186 | int num; | ||
187 | int maxnum; | ||
188 | }; | ||
189 | |||
190 | int map_parse_line_cb(int n, char* buf, void* arg) | ||
191 | { | ||
192 | (void)n; | ||
193 | |||
194 | struct map_parse_args* args = arg; | ||
195 | |||
196 | /* ignore comments and blank lines */ | ||
197 | if(*buf == '#' || *buf == '\0') | ||
198 | return 0; | ||
199 | |||
200 | struct xf_map dummy_map; | ||
201 | struct xf_map* dst_map; | ||
202 | if(args->num < args->maxnum) | ||
203 | dst_map = &args->map[args->num]; | ||
204 | else | ||
205 | dst_map = &dummy_map; | ||
206 | |||
207 | int rc = xf_map_parseline(buf, dst_map); | ||
208 | if(rc) | ||
209 | return rc; | ||
210 | |||
211 | args->num++; | ||
212 | return 0; | ||
213 | } | ||
214 | |||
215 | int xf_map_parse(struct xf_stream* s, struct xf_map* map, int maxnum) | ||
216 | { | ||
217 | char buf[200]; | ||
218 | struct map_parse_args args; | ||
219 | args.map = map; | ||
220 | args.num = 0; | ||
221 | args.maxnum = maxnum; | ||
222 | |||
223 | int rc = xf_stream_read_lines(s, buf, sizeof(buf), | ||
224 | map_parse_line_cb, &args); | ||
225 | if(rc < 0) | ||
226 | return rc; | ||
227 | |||
228 | return args.num; | ||
229 | } | ||
230 | |||
231 | static int xf_map_compare(const void* a, const void* b) | ||
232 | { | ||
233 | const struct xf_map* mapA = a; | ||
234 | const struct xf_map* mapB = b; | ||
235 | |||
236 | if(mapA->offset < mapB->offset) | ||
237 | return -1; | ||
238 | else if(mapA->offset == mapB->offset) | ||
239 | return 0; | ||
240 | else | ||
241 | return 1; | ||
242 | } | ||
243 | |||
244 | int xf_map_sort(struct xf_map* map, int num) | ||
245 | { | ||
246 | qsort(map, num, sizeof(struct xf_map), xf_map_compare); | ||
247 | return xf_map_validate(map, num); | ||
248 | } | ||
249 | |||
250 | int xf_map_validate(const struct xf_map* map, int num) | ||
251 | { | ||
252 | for(int i = 1; i < num; ++i) | ||
253 | if(map[i].offset <= map[i-1].offset) | ||
254 | return -1; | ||
255 | |||
256 | for(int i = 1; i < num; ++i) | ||
257 | if(map[i-1].offset + map[i-1].length > map[i].offset) | ||
258 | return i; | ||
259 | |||
260 | return 0; | ||
261 | } | ||
262 | |||
263 | int xf_map_write(struct xf_map* map, int num, struct xf_stream* s) | ||
264 | { | ||
265 | static const char hex[] = "0123456789abcdef"; | ||
266 | char buf[200]; | ||
267 | char md5str[33]; | ||
268 | int total_len = 0; | ||
269 | |||
270 | md5str[32] = '\0'; | ||
271 | |||
272 | for(int i = 0; i < num; ++i) { | ||
273 | bool has_md5 = false; | ||
274 | if(map->flags & XF_MAP_HAS_MD5) { | ||
275 | if(!(map->flags & XF_MAP_HAS_FILE_LENGTH)) | ||
276 | return XF_E_INVALID_PARAMETER; | ||
277 | |||
278 | has_md5 = true; | ||
279 | for(int j = 0; j < 16; ++j) { | ||
280 | uint8_t byte = map[i].md5[j]; | ||
281 | md5str[2*j] = hex[(byte >> 4) & 0xf]; | ||
282 | md5str[2*j+1] = hex[byte & 0xf]; | ||
283 | } | ||
284 | } | ||
285 | |||
286 | int len; | ||
287 | if(!has_md5) { | ||
288 | len = snprintf(buf, sizeof(buf), "%s - %lx %lu\n", | ||
289 | map[i].name, | ||
290 | (unsigned long)map[i].offset, | ||
291 | (unsigned long)map[i].length); | ||
292 | } else { | ||
293 | len = snprintf(buf, sizeof(buf), "%s %s %lu 0x%lx %lu\n", | ||
294 | map[i].name, md5str, | ||
295 | (unsigned long)map[i].file_length, | ||
296 | (unsigned long)map[i].offset, | ||
297 | (unsigned long)map[i].length); | ||
298 | } | ||
299 | |||
300 | if(len < 0 || (size_t)len >= sizeof(buf)) | ||
301 | return XF_E_LINE_TOO_LONG; | ||
302 | |||
303 | if(s) { | ||
304 | int rc = xf_stream_write(s, buf, len); | ||
305 | if(rc != len) | ||
306 | return XF_E_IO; | ||
307 | } | ||
308 | |||
309 | total_len += len; | ||
310 | } | ||
311 | |||
312 | return total_len; | ||
313 | } | ||
diff --git a/lib/x1000-installer/src/xf_nandio.c b/lib/x1000-installer/src/xf_nandio.c new file mode 100644 index 0000000000..ba79cbbcbf --- /dev/null +++ b/lib/x1000-installer/src/xf_nandio.c | |||
@@ -0,0 +1,295 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2021 Aidan MacDonald | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "xf_nandio.h" | ||
23 | #include "xf_error.h" | ||
24 | #include "core_alloc.h" | ||
25 | #include "system.h" | ||
26 | #include <string.h> | ||
27 | #include <stdbool.h> | ||
28 | |||
29 | int xf_nandio_init(struct xf_nandio* nio) | ||
30 | { | ||
31 | int rc; | ||
32 | |||
33 | memset(nio, 0, sizeof(*nio)); | ||
34 | |||
35 | /* open NAND */ | ||
36 | nio->ndrv = nand_init(); | ||
37 | nand_lock(nio->ndrv); | ||
38 | rc = nand_open(nio->ndrv); | ||
39 | if(rc != NAND_SUCCESS) { | ||
40 | nio->nand_err = rc; | ||
41 | rc = XF_E_NAND; | ||
42 | goto out; | ||
43 | } | ||
44 | |||
45 | /* read chip parameters */ | ||
46 | nio->page_size = nio->ndrv->chip->page_size; | ||
47 | nio->block_size = nio->page_size << nio->ndrv->chip->log2_ppb; | ||
48 | |||
49 | /* allocate memory */ | ||
50 | size_t alloc_size = 0; | ||
51 | alloc_size += CACHEALIGN_SIZE - 1; | ||
52 | alloc_size += nio->block_size * 2; | ||
53 | |||
54 | nio->alloc_handle = core_alloc("xf_nandio", alloc_size); | ||
55 | if(nio->alloc_handle < 0) { | ||
56 | rc = XF_E_OUT_OF_MEMORY; | ||
57 | goto out_nclose; | ||
58 | } | ||
59 | |||
60 | uint8_t* buffer = core_get_data(nio->alloc_handle); | ||
61 | CACHEALIGN_BUFFER(buffer, alloc_size); | ||
62 | |||
63 | nio->old_buf = buffer; | ||
64 | nio->new_buf = &buffer[nio->block_size]; | ||
65 | |||
66 | rc = XF_E_SUCCESS; | ||
67 | goto out; | ||
68 | |||
69 | out_nclose: | ||
70 | nand_close(nio->ndrv); | ||
71 | out: | ||
72 | nand_unlock(nio->ndrv); | ||
73 | return rc; | ||
74 | } | ||
75 | |||
76 | void xf_nandio_destroy(struct xf_nandio* nio) | ||
77 | { | ||
78 | if(nio->alloc_handle > 0) { | ||
79 | core_free(nio->alloc_handle); | ||
80 | nio->alloc_handle = 0; | ||
81 | } | ||
82 | |||
83 | if(nio->ndrv) { | ||
84 | nand_lock(nio->ndrv); | ||
85 | nand_close(nio->ndrv); | ||
86 | nand_unlock(nio->ndrv); | ||
87 | nio->ndrv = NULL; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | static bool is_page_blank(const uint8_t* buf, uint32_t length) | ||
92 | { | ||
93 | for(uint32_t i = 0; i < length; ++i) | ||
94 | if(buf[i] != 0xff) | ||
95 | return false; | ||
96 | |||
97 | return true; | ||
98 | } | ||
99 | |||
100 | static int flush_block(struct xf_nandio* nio, bool invalidate) | ||
101 | { | ||
102 | /* no block, or only reading - flush is a no-op */ | ||
103 | if(!nio->block_valid || nio->mode == XF_NANDIO_READ) | ||
104 | return XF_E_SUCCESS; | ||
105 | |||
106 | /* nothing to do if new data is same as old data */ | ||
107 | if(!memcmp(nio->old_buf, nio->new_buf, nio->block_size)) | ||
108 | return XF_E_SUCCESS; | ||
109 | |||
110 | /* data mismatch during verification - report the error */ | ||
111 | if(nio->mode == XF_NANDIO_VERIFY) | ||
112 | return XF_E_VERIFY_FAILED; | ||
113 | |||
114 | /* erase the block */ | ||
115 | int rc = nand_block_erase(nio->ndrv, nio->cur_block); | ||
116 | if(rc != NAND_SUCCESS) { | ||
117 | nio->block_valid = false; | ||
118 | nio->nand_err = rc; | ||
119 | return XF_E_NAND; | ||
120 | } | ||
121 | |||
122 | size_t oob_size = nio->ndrv->chip->oob_size; | ||
123 | |||
124 | unsigned page = 0; | ||
125 | nand_page_t page_addr = nio->cur_block; | ||
126 | for(; page < nio->ndrv->ppb; ++page, ++page_addr) { | ||
127 | /* skip programming blank pages to go faster & reduce wear */ | ||
128 | uint8_t* page_data = &nio->new_buf[page * nio->page_size]; | ||
129 | if(is_page_blank(page_data, nio->page_size)) | ||
130 | continue; | ||
131 | |||
132 | /* copy page and write blank OOB data */ | ||
133 | memcpy(nio->ndrv->page_buf, page_data, nio->page_size); | ||
134 | memset(&nio->ndrv->page_buf[nio->page_size], 0xff, oob_size); | ||
135 | |||
136 | /* program the page */ | ||
137 | rc = nand_page_program(nio->ndrv, page_addr, nio->ndrv->page_buf); | ||
138 | if(rc != NAND_SUCCESS) { | ||
139 | nio->block_valid = false; | ||
140 | nio->nand_err = rc; | ||
141 | return XF_E_NAND; | ||
142 | } | ||
143 | } | ||
144 | |||
145 | if(invalidate) | ||
146 | nio->block_valid = false; | ||
147 | else { | ||
148 | /* update our 'old' buffer so a subsequent flush | ||
149 | * will not reprogram the same block */ | ||
150 | memcpy(nio->old_buf, nio->new_buf, nio->block_size); | ||
151 | } | ||
152 | |||
153 | return XF_E_SUCCESS; | ||
154 | } | ||
155 | |||
156 | static int seek_to_block(struct xf_nandio* nio, nand_block_t block_addr, | ||
157 | size_t offset_in_block) | ||
158 | { | ||
159 | /* already on this block? */ | ||
160 | if(nio->block_valid && block_addr == nio->cur_block) { | ||
161 | nio->offset_in_block = offset_in_block; | ||
162 | return XF_E_SUCCESS; | ||
163 | } | ||
164 | |||
165 | /* ensure new block is within range */ | ||
166 | if(block_addr >= (nio->ndrv->chip->nr_blocks << nio->ndrv->chip->log2_ppb)) | ||
167 | return XF_E_OUT_OF_RANGE; | ||
168 | |||
169 | /* flush old block */ | ||
170 | int rc = flush_block(nio, true); | ||
171 | if(rc) | ||
172 | return rc; | ||
173 | |||
174 | nio->block_valid = false; | ||
175 | |||
176 | /* read the new block */ | ||
177 | unsigned page = 0; | ||
178 | nand_page_t page_addr = block_addr; | ||
179 | for(; page < nio->ndrv->ppb; ++page, ++page_addr) { | ||
180 | rc = nand_page_read(nio->ndrv, page_addr, nio->ndrv->page_buf); | ||
181 | if(rc != NAND_SUCCESS) { | ||
182 | nio->nand_err = rc; | ||
183 | return XF_E_NAND; | ||
184 | } | ||
185 | |||
186 | memcpy(&nio->old_buf[page * nio->page_size], nio->ndrv->page_buf, nio->page_size); | ||
187 | } | ||
188 | |||
189 | /* copy to 2nd buffer */ | ||
190 | memcpy(nio->new_buf, nio->old_buf, nio->block_size); | ||
191 | |||
192 | /* update position */ | ||
193 | nio->cur_block = block_addr; | ||
194 | nio->offset_in_block = offset_in_block; | ||
195 | nio->block_valid = true; | ||
196 | return XF_E_SUCCESS; | ||
197 | } | ||
198 | |||
199 | int xf_nandio_set_mode(struct xf_nandio* nio, enum xf_nandio_mode mode) | ||
200 | { | ||
201 | nand_lock(nio->ndrv); | ||
202 | |||
203 | /* flush the current block before switching to the new mode, | ||
204 | * to ensure consistency */ | ||
205 | int rc = flush_block(nio, false); | ||
206 | if(rc) | ||
207 | goto err; | ||
208 | |||
209 | nio->mode = mode; | ||
210 | rc = XF_E_SUCCESS; | ||
211 | |||
212 | err: | ||
213 | nand_unlock(nio->ndrv); | ||
214 | return rc; | ||
215 | } | ||
216 | |||
217 | static int nandio_rdwr(struct xf_nandio* nio, void* buf, size_t count, bool write) | ||
218 | { | ||
219 | while(count > 0) { | ||
220 | void* ptr; | ||
221 | size_t amount = count; | ||
222 | int rc = xf_nandio_get_buffer(nio, &ptr, &amount); | ||
223 | if(rc) | ||
224 | return rc; | ||
225 | |||
226 | if(write) | ||
227 | memcpy(ptr, buf, amount); | ||
228 | else | ||
229 | memcpy(buf, ptr, amount); | ||
230 | |||
231 | count -= amount; | ||
232 | } | ||
233 | |||
234 | return XF_E_SUCCESS; | ||
235 | } | ||
236 | |||
237 | int xf_nandio_seek(struct xf_nandio* nio, size_t offset) | ||
238 | { | ||
239 | uint32_t block_nr = offset / nio->block_size; | ||
240 | size_t offset_in_block = offset % nio->block_size; | ||
241 | nand_block_t block_addr = block_nr << nio->ndrv->chip->log2_ppb; | ||
242 | |||
243 | nand_lock(nio->ndrv); | ||
244 | int rc = seek_to_block(nio, block_addr, offset_in_block); | ||
245 | nand_unlock(nio->ndrv); | ||
246 | |||
247 | return rc; | ||
248 | } | ||
249 | |||
250 | int xf_nandio_read(struct xf_nandio* nio, void* buf, size_t count) | ||
251 | { | ||
252 | return nandio_rdwr(nio, buf, count, false); | ||
253 | } | ||
254 | |||
255 | int xf_nandio_write(struct xf_nandio* nio, const void* buf, size_t count) | ||
256 | { | ||
257 | return nandio_rdwr(nio, (void*)buf, count, true); | ||
258 | } | ||
259 | |||
260 | int xf_nandio_get_buffer(struct xf_nandio* nio, void** buf, size_t* count) | ||
261 | { | ||
262 | nand_lock(nio->ndrv); | ||
263 | |||
264 | /* make sure the current block data is read in */ | ||
265 | int rc = seek_to_block(nio, nio->cur_block, nio->offset_in_block); | ||
266 | if(rc) | ||
267 | goto err; | ||
268 | |||
269 | size_t amount_left = nio->block_size - nio->offset_in_block; | ||
270 | if(amount_left == 0) { | ||
271 | amount_left = nio->block_size; | ||
272 | rc = seek_to_block(nio, nio->cur_block + nio->ndrv->ppb, 0); | ||
273 | if(rc) | ||
274 | goto err; | ||
275 | } | ||
276 | |||
277 | *buf = &nio->new_buf[nio->offset_in_block]; | ||
278 | *count = MIN(*count, amount_left); | ||
279 | |||
280 | nio->offset_in_block += *count; | ||
281 | rc = XF_E_SUCCESS; | ||
282 | |||
283 | err: | ||
284 | nand_unlock(nio->ndrv); | ||
285 | return rc; | ||
286 | } | ||
287 | |||
288 | int xf_nandio_flush(struct xf_nandio* nio) | ||
289 | { | ||
290 | nand_lock(nio->ndrv); | ||
291 | int rc = flush_block(nio, false); | ||
292 | nand_unlock(nio->ndrv); | ||
293 | |||
294 | return rc; | ||
295 | } | ||
diff --git a/lib/x1000-installer/src/xf_package.c b/lib/x1000-installer/src/xf_package.c new file mode 100644 index 0000000000..78bddded68 --- /dev/null +++ b/lib/x1000-installer/src/xf_package.c | |||
@@ -0,0 +1,264 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2021 Aidan MacDonald | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "xf_package.h" | ||
23 | #include "xf_error.h" | ||
24 | #include "pathfuncs.h" | ||
25 | #include "file.h" | ||
26 | #include "core_alloc.h" | ||
27 | #include "md5.h" | ||
28 | #include "system.h" | ||
29 | #include <stdbool.h> | ||
30 | #include <string.h> | ||
31 | |||
32 | #ifdef ROCKBOX | ||
33 | # include "microtar-rockbox.h" | ||
34 | #else | ||
35 | # include "microtar-stdio.h" | ||
36 | #endif | ||
37 | |||
38 | #define METADATA_SIZE 4096 /* size of the metadata buffer */ | ||
39 | #define MAX_MAP_SIZE 32 /* maximum number of map entries */ | ||
40 | |||
41 | static int pkg_alloc(struct xf_package* pkg) | ||
42 | { | ||
43 | memset(pkg, 0, sizeof(*pkg)); | ||
44 | |||
45 | /* calculate allocation size */ | ||
46 | size_t alloc_size = 0; | ||
47 | alloc_size += ALIGN_UP_P2(sizeof(mtar_t), 3); | ||
48 | alloc_size += ALIGN_UP_P2(sizeof(struct xf_map) * MAX_MAP_SIZE, 3); | ||
49 | alloc_size += ALIGN_UP_P2(METADATA_SIZE, 3); | ||
50 | alloc_size += 7; /* for alignment */ | ||
51 | |||
52 | pkg->alloc_handle = core_alloc("xf_package", alloc_size); | ||
53 | if(pkg->alloc_handle < 0) | ||
54 | return XF_E_OUT_OF_MEMORY; | ||
55 | |||
56 | /* distribute memory */ | ||
57 | uint8_t* buf = (uint8_t*)core_get_data(pkg->alloc_handle); | ||
58 | memset(buf, 0, alloc_size); | ||
59 | ALIGN_BUFFER(buf, alloc_size, 8); | ||
60 | |||
61 | pkg->tar = (mtar_t*)buf; | ||
62 | buf += ALIGN_UP_P2(sizeof(mtar_t), 3); | ||
63 | |||
64 | pkg->map = (struct xf_map*)buf; | ||
65 | buf += ALIGN_UP_P2(sizeof(struct xf_map) * MAX_MAP_SIZE, 3); | ||
66 | |||
67 | pkg->metadata = (char*)buf; | ||
68 | buf += ALIGN_UP_P2(METADATA_SIZE, 3); | ||
69 | |||
70 | return XF_E_SUCCESS; | ||
71 | } | ||
72 | |||
73 | static int read_meta_line_cb(int n, char* buf, void* arg) | ||
74 | { | ||
75 | struct xf_package* pkg = (struct xf_package*)arg; | ||
76 | size_t length = strlen(buf); | ||
77 | |||
78 | /* skip blank lines and the first line (it's reserved for old format) */ | ||
79 | if(n == 0 || length == 0) | ||
80 | return 0; | ||
81 | |||
82 | /* metadata lines require an '=' sign to separate key and value */ | ||
83 | if(!strchr(buf, '=')) | ||
84 | return XF_E_SYNTAX_ERROR; | ||
85 | |||
86 | /* we need space to copy the key-value pair plus a null terminator */ | ||
87 | if(length + 1 >= METADATA_SIZE - pkg->metadata_len) | ||
88 | return XF_E_BUF_OVERFLOW; | ||
89 | |||
90 | memcpy(&pkg->metadata[pkg->metadata_len], buf, length + 1); | ||
91 | pkg->metadata_len += length + 1; | ||
92 | return 0; | ||
93 | } | ||
94 | |||
95 | static int pkg_read_meta(struct xf_package* pkg) | ||
96 | { | ||
97 | struct xf_stream stream; | ||
98 | int rc = xf_open_tar(pkg->tar, "bootloader-info.txt", &stream); | ||
99 | if(rc) | ||
100 | return XF_E_CANNOT_OPEN_FILE; | ||
101 | |||
102 | char buf[200]; | ||
103 | rc = xf_stream_read_lines(&stream, buf, sizeof(buf), read_meta_line_cb, pkg); | ||
104 | xf_stream_close(&stream); | ||
105 | return rc; | ||
106 | } | ||
107 | |||
108 | static int pkg_read_map(struct xf_package* pkg, | ||
109 | const struct xf_map* dflt_map, int dflt_map_size) | ||
110 | { | ||
111 | /* Attempt to load and parse the map file */ | ||
112 | struct xf_stream stream; | ||
113 | int rc = xf_open_tar(pkg->tar, "flashmap.txt", &stream); | ||
114 | |||
115 | /* If the flash map is absent but a default map has been provided, | ||
116 | * then the update is in the old fixed format. */ | ||
117 | if(rc == MTAR_ENOTFOUND && dflt_map) { | ||
118 | if(dflt_map_size > MAX_MAP_SIZE) | ||
119 | return XF_E_INVALID_PARAMETER; | ||
120 | |||
121 | for(int i = 0; i < dflt_map_size; ++i) { | ||
122 | pkg->map[i] = dflt_map[i]; | ||
123 | pkg->map[i].flags &= ~(XF_MAP_HAS_MD5 | XF_MAP_HAS_FILE_LENGTH); | ||
124 | } | ||
125 | |||
126 | pkg->map_size = dflt_map_size; | ||
127 | return XF_E_SUCCESS; | ||
128 | } | ||
129 | |||
130 | if(rc != MTAR_ESUCCESS) | ||
131 | return XF_E_CANNOT_OPEN_FILE; | ||
132 | |||
133 | rc = xf_map_parse(&stream, pkg->map, MAX_MAP_SIZE); | ||
134 | if(rc < 0) | ||
135 | goto err; | ||
136 | |||
137 | /* Sort the map; reject it if there is any overlap. */ | ||
138 | pkg->map_size = rc; | ||
139 | if(xf_map_sort(pkg->map, pkg->map_size)) { | ||
140 | rc = XF_E_INVALID_PARAMETER; | ||
141 | goto err; | ||
142 | } | ||
143 | |||
144 | /* All packages in the 'new' format are required to have MD5 sums. */ | ||
145 | for(int i = 0; i < pkg->map_size; ++i) { | ||
146 | if(!(pkg->map[i].flags & XF_MAP_HAS_MD5) || | ||
147 | !(pkg->map[i].flags & XF_MAP_HAS_FILE_LENGTH)) { | ||
148 | rc = XF_E_VERIFY_FAILED; | ||
149 | goto err; | ||
150 | } | ||
151 | } | ||
152 | |||
153 | rc = XF_E_SUCCESS; | ||
154 | |||
155 | err: | ||
156 | xf_stream_close(&stream); | ||
157 | return rc; | ||
158 | } | ||
159 | |||
160 | static int pkg_verify(struct xf_package* pkg) | ||
161 | { | ||
162 | struct xf_stream stream; | ||
163 | md5_context ctx; | ||
164 | uint8_t buffer[128]; | ||
165 | |||
166 | for(int i = 0; i < pkg->map_size; ++i) { | ||
167 | /* At a bare minimum, check that the file exists. */ | ||
168 | int rc = xf_open_tar(pkg->tar, pkg->map[i].name, &stream); | ||
169 | if(rc) | ||
170 | return XF_E_VERIFY_FAILED; | ||
171 | |||
172 | /* Also check that it isn't bigger than the update region. | ||
173 | * That would normally indicate a problem. */ | ||
174 | off_t streamsize = xf_stream_get_size(&stream); | ||
175 | if(streamsize > (off_t)pkg->map[i].length) { | ||
176 | rc = XF_E_VERIFY_FAILED; | ||
177 | goto err; | ||
178 | } | ||
179 | |||
180 | /* Check against the listed file length. */ | ||
181 | if(pkg->map[i].flags & XF_MAP_HAS_FILE_LENGTH) { | ||
182 | if(streamsize != (off_t)pkg->map[i].file_length) { | ||
183 | rc = XF_E_VERIFY_FAILED; | ||
184 | goto err; | ||
185 | } | ||
186 | } | ||
187 | |||
188 | /* Check the MD5 sum if we have it. */ | ||
189 | if(pkg->map[i].flags & XF_MAP_HAS_MD5) { | ||
190 | md5_starts(&ctx); | ||
191 | while(1) { | ||
192 | ssize_t n = xf_stream_read(&stream, buffer, sizeof(buffer)); | ||
193 | if(n < 0) { | ||
194 | rc = XF_E_IO; | ||
195 | goto err; | ||
196 | } | ||
197 | |||
198 | md5_update(&ctx, buffer, n); | ||
199 | if((size_t)n < sizeof(buffer)) | ||
200 | break; | ||
201 | } | ||
202 | |||
203 | md5_finish(&ctx, buffer); | ||
204 | if(memcpy(buffer, pkg->map[i].md5, 16)) { | ||
205 | rc = XF_E_VERIFY_FAILED; | ||
206 | goto err; | ||
207 | } | ||
208 | } | ||
209 | |||
210 | err: | ||
211 | xf_stream_close(&stream); | ||
212 | if(rc) | ||
213 | return rc; | ||
214 | } | ||
215 | |||
216 | /* All files passed verification */ | ||
217 | return XF_E_SUCCESS; | ||
218 | } | ||
219 | |||
220 | int xf_package_open_ex(struct xf_package* pkg, const char* file, | ||
221 | const struct xf_map* dflt_map, int dflt_map_size) | ||
222 | { | ||
223 | int rc = pkg_alloc(pkg); | ||
224 | if(rc) | ||
225 | return rc; | ||
226 | |||
227 | #ifdef ROCKBOX | ||
228 | rc = mtar_open(pkg->tar, file, O_RDONLY); | ||
229 | #else | ||
230 | rc = mtar_open(pkg->tar, file, "r"); | ||
231 | #endif | ||
232 | if(rc != MTAR_ESUCCESS) { | ||
233 | rc = XF_E_CANNOT_OPEN_FILE; | ||
234 | goto err; | ||
235 | } | ||
236 | |||
237 | rc = pkg_read_meta(pkg); | ||
238 | if(rc) | ||
239 | goto err; | ||
240 | |||
241 | rc = pkg_read_map(pkg, dflt_map, dflt_map_size); | ||
242 | if(rc) | ||
243 | goto err; | ||
244 | |||
245 | rc = pkg_verify(pkg); | ||
246 | if(rc) | ||
247 | goto err; | ||
248 | |||
249 | err: | ||
250 | if(rc) | ||
251 | xf_package_close(pkg); | ||
252 | return rc; | ||
253 | } | ||
254 | |||
255 | void xf_package_close(struct xf_package* pkg) | ||
256 | { | ||
257 | if(mtar_is_open(pkg->tar)) | ||
258 | mtar_close(pkg->tar); | ||
259 | |||
260 | if(pkg->alloc_handle > 0) { | ||
261 | core_free(pkg->alloc_handle); | ||
262 | pkg->alloc_handle = 0; | ||
263 | } | ||
264 | } | ||
diff --git a/lib/x1000-installer/src/xf_stream.c b/lib/x1000-installer/src/xf_stream.c new file mode 100644 index 0000000000..5a0f86123c --- /dev/null +++ b/lib/x1000-installer/src/xf_stream.c | |||
@@ -0,0 +1,214 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2021 Aidan MacDonald | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "xf_stream.h" | ||
23 | #include "xf_error.h" | ||
24 | #include "file.h" | ||
25 | #include <stdbool.h> | ||
26 | #include <string.h> | ||
27 | #include <ctype.h> | ||
28 | |||
29 | /* | ||
30 | * File streams | ||
31 | */ | ||
32 | |||
33 | static off_t file_stream_get_size(struct xf_stream* s) | ||
34 | { | ||
35 | return filesize(s->data); | ||
36 | } | ||
37 | |||
38 | static ssize_t file_stream_read(struct xf_stream* s, void* buf, size_t len) | ||
39 | { | ||
40 | return read(s->data, buf, len); | ||
41 | } | ||
42 | |||
43 | static ssize_t file_stream_write(struct xf_stream* s, const void* buf, size_t len) | ||
44 | { | ||
45 | return write(s->data, buf, len); | ||
46 | } | ||
47 | |||
48 | static int file_stream_close(struct xf_stream* s) | ||
49 | { | ||
50 | return close(s->data); | ||
51 | } | ||
52 | |||
53 | static const struct xf_stream_ops file_stream_ops = { | ||
54 | .xget_size = file_stream_get_size, | ||
55 | .xread = file_stream_read, | ||
56 | .xwrite = file_stream_write, | ||
57 | .xclose = file_stream_close, | ||
58 | }; | ||
59 | |||
60 | int xf_open_file(const char* file, int flags, struct xf_stream* s) | ||
61 | { | ||
62 | s->data = open(file, flags, 0666); | ||
63 | s->ops = &file_stream_ops; | ||
64 | return (s->data >= 0) ? 0 : -1; | ||
65 | } | ||
66 | |||
67 | /* | ||
68 | * Tar streams | ||
69 | */ | ||
70 | |||
71 | static off_t tar_stream_get_size(struct xf_stream* s) | ||
72 | { | ||
73 | mtar_t* mtar = (mtar_t*)s->data; | ||
74 | return mtar_get_header(mtar)->size; | ||
75 | } | ||
76 | |||
77 | static ssize_t tar_stream_read(struct xf_stream* s, void* buffer, size_t count) | ||
78 | { | ||
79 | mtar_t* mtar = (mtar_t*)s->data; | ||
80 | |||
81 | int ret = mtar_read_data(mtar, buffer, count); | ||
82 | if(ret < 0) | ||
83 | return -1; | ||
84 | |||
85 | return ret; | ||
86 | } | ||
87 | |||
88 | static ssize_t tar_stream_write(struct xf_stream* s, const void* buffer, size_t count) | ||
89 | { | ||
90 | mtar_t* mtar = (mtar_t*)s->data; | ||
91 | |||
92 | int ret = mtar_write_data(mtar, buffer, count); | ||
93 | if(ret < 0) | ||
94 | return -1; | ||
95 | |||
96 | return ret; | ||
97 | } | ||
98 | |||
99 | static int tar_stream_close(struct xf_stream* s) | ||
100 | { | ||
101 | mtar_t* mtar = (mtar_t*)s->data; | ||
102 | |||
103 | if(mtar_access_mode(mtar) == MTAR_WRITE) { | ||
104 | if(mtar_update_file_size(mtar) != MTAR_ESUCCESS) | ||
105 | return -1; | ||
106 | if(mtar_end_data(mtar) != MTAR_ESUCCESS) | ||
107 | return -1; | ||
108 | } | ||
109 | |||
110 | return 0; | ||
111 | } | ||
112 | |||
113 | static const struct xf_stream_ops tar_stream_ops = { | ||
114 | .xget_size = tar_stream_get_size, | ||
115 | .xread = tar_stream_read, | ||
116 | .xwrite = tar_stream_write, | ||
117 | .xclose = tar_stream_close, | ||
118 | }; | ||
119 | |||
120 | int xf_open_tar(mtar_t* mtar, const char* file, struct xf_stream* s) | ||
121 | { | ||
122 | int err = mtar_find(mtar, file); | ||
123 | if(err != MTAR_ESUCCESS) | ||
124 | return err; | ||
125 | |||
126 | /* must only read normal files */ | ||
127 | const mtar_header_t* h = mtar_get_header(mtar); | ||
128 | if(h->type != 0 && h->type != MTAR_TREG) | ||
129 | return MTAR_EFAILURE; | ||
130 | |||
131 | s->data = (intptr_t)mtar; | ||
132 | s->ops = &tar_stream_ops; | ||
133 | return MTAR_ESUCCESS; | ||
134 | } | ||
135 | |||
136 | int xf_create_tar(mtar_t* mtar, const char* file, size_t size, struct xf_stream* s) | ||
137 | { | ||
138 | int err = mtar_write_file_header(mtar, file, size); | ||
139 | if(err) | ||
140 | return err; | ||
141 | |||
142 | s->data = (intptr_t)mtar; | ||
143 | s->ops = &tar_stream_ops; | ||
144 | return MTAR_ESUCCESS; | ||
145 | } | ||
146 | |||
147 | /* | ||
148 | * Utility functions | ||
149 | */ | ||
150 | |||
151 | int xf_stream_read_lines(struct xf_stream* s, char* buf, size_t bufsz, | ||
152 | int(*callback)(int n, char* buf, void* arg), void* arg) | ||
153 | { | ||
154 | char* startp, *endp; | ||
155 | ssize_t bytes_read; | ||
156 | int rc; | ||
157 | |||
158 | int n = 0; | ||
159 | size_t pos = 0; | ||
160 | bool at_eof = false; | ||
161 | |||
162 | while(!at_eof) { | ||
163 | bytes_read = xf_stream_read(s, &buf[pos], bufsz - pos - 1); | ||
164 | if(bytes_read < 0) | ||
165 | return XF_E_IO; | ||
166 | |||
167 | /* short read is end of file */ | ||
168 | if((size_t)bytes_read < bufsz - pos - 1) | ||
169 | at_eof = true; | ||
170 | |||
171 | pos += bytes_read; | ||
172 | buf[pos] = '\0'; | ||
173 | |||
174 | startp = endp = buf; | ||
175 | while(endp != &buf[pos]) { | ||
176 | endp = strchr(startp, '\n'); | ||
177 | if(endp) { | ||
178 | *endp = '\0'; | ||
179 | } else { | ||
180 | if(!at_eof) { | ||
181 | if(startp == buf) | ||
182 | return XF_E_LINE_TOO_LONG; | ||
183 | else | ||
184 | break; /* read ahead to look for newline */ | ||
185 | } else { | ||
186 | endp = &buf[pos]; /* treat EOF as a newline */ | ||
187 | } | ||
188 | } | ||
189 | |||
190 | /* skip whitespace */ | ||
191 | while(*startp && isspace(*startp)) | ||
192 | ++startp; | ||
193 | |||
194 | /* ignore blank lines and comment lines */ | ||
195 | if(*startp == '#' || *startp == '\0') { | ||
196 | startp = endp + 1; | ||
197 | continue; | ||
198 | } | ||
199 | |||
200 | rc = callback(n++, startp, arg); | ||
201 | if(rc != 0) | ||
202 | return rc; | ||
203 | |||
204 | startp = endp + 1; | ||
205 | } | ||
206 | |||
207 | if(startp <= &buf[pos]) { | ||
208 | memmove(buf, startp, &buf[pos] - startp); | ||
209 | pos = &buf[pos] - startp; | ||
210 | } | ||
211 | } | ||
212 | |||
213 | return 0; | ||
214 | } | ||
diff --git a/lib/x1000-installer/src/xf_update.c b/lib/x1000-installer/src/xf_update.c new file mode 100644 index 0000000000..5a7c3b0430 --- /dev/null +++ b/lib/x1000-installer/src/xf_update.c | |||
@@ -0,0 +1,149 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2021 Aidan MacDonald | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "xf_update.h" | ||
23 | #include "xf_error.h" | ||
24 | #include "file.h" | ||
25 | #include "md5.h" | ||
26 | #include <string.h> | ||
27 | |||
28 | static int process_stream(struct xf_nandio* nio, | ||
29 | struct xf_map* map, | ||
30 | struct xf_stream* stream) | ||
31 | { | ||
32 | void* buffer; | ||
33 | size_t count; | ||
34 | int rc; | ||
35 | |||
36 | /* calculate MD5 on the fly if taking a backup */ | ||
37 | md5_context md5_ctx; | ||
38 | if(nio->mode == XF_NANDIO_READ) | ||
39 | md5_starts(&md5_ctx); | ||
40 | |||
41 | /* first deal with the file data */ | ||
42 | size_t bytes_left = map->length; | ||
43 | while(bytes_left > 0) { | ||
44 | count = bytes_left; | ||
45 | rc = xf_nandio_get_buffer(nio, &buffer, &count); | ||
46 | if(rc) | ||
47 | return rc; | ||
48 | |||
49 | if(nio->mode == XF_NANDIO_READ) { | ||
50 | md5_update(&md5_ctx, buffer, count); | ||
51 | rc = xf_stream_write(stream, buffer, count); | ||
52 | } else { | ||
53 | rc = xf_stream_read(stream, buffer, count); | ||
54 | } | ||
55 | |||
56 | bytes_left -= count; | ||
57 | |||
58 | if(rc < 0 || (size_t)rc > count) | ||
59 | return XF_E_IO; | ||
60 | |||
61 | if((size_t)rc < count) { | ||
62 | /* backup - we could not write all the data */ | ||
63 | if(nio->mode == XF_NANDIO_READ) | ||
64 | return XF_E_IO; | ||
65 | |||
66 | /* update - clear rest of buffer to 0xff */ | ||
67 | memset(buffer + rc, 0xff, count - rc); | ||
68 | break; | ||
69 | } | ||
70 | } | ||
71 | |||
72 | /* if updating - write blanks to the remainder of the region */ | ||
73 | while(bytes_left > 0) { | ||
74 | count = bytes_left; | ||
75 | rc = xf_nandio_get_buffer(nio, &buffer, &count); | ||
76 | if(rc) | ||
77 | return rc; | ||
78 | |||
79 | memset(buffer, 0xff, count); | ||
80 | bytes_left -= count; | ||
81 | } | ||
82 | |||
83 | /* finalize the MD5 sum */ | ||
84 | if(nio->mode == XF_NANDIO_READ) { | ||
85 | md5_finish(&md5_ctx, map->md5); | ||
86 | map->file_length = map->length; | ||
87 | map->flags |= XF_MAP_HAS_MD5 | XF_MAP_HAS_FILE_LENGTH; | ||
88 | } | ||
89 | |||
90 | return XF_E_SUCCESS; | ||
91 | } | ||
92 | |||
93 | static int process_map(struct xf_nandio* nio, struct xf_map* map, int map_size, | ||
94 | xf_update_open_stream_cb open_stream, void* os_arg) | ||
95 | { | ||
96 | int rc, rc2; | ||
97 | struct xf_stream stream; | ||
98 | |||
99 | /* ensure the map is sequential and non-overlapping before continuing */ | ||
100 | if(xf_map_validate(map, map_size) != 0) | ||
101 | return XF_E_INVALID_PARAMETER; | ||
102 | |||
103 | for(int i = 0; i < map_size; ++i) { | ||
104 | /* seek to initial offset */ | ||
105 | rc = xf_nandio_seek(nio, map[i].offset); | ||
106 | if(rc) | ||
107 | return rc; | ||
108 | |||
109 | rc = open_stream(os_arg, map[i].name, &stream); | ||
110 | if(rc) | ||
111 | return XF_E_CANNOT_OPEN_FILE; | ||
112 | |||
113 | /* process the stream and be sure to close it even on error */ | ||
114 | rc = process_stream(nio, &map[i], &stream); | ||
115 | rc2 = xf_stream_close(&stream); | ||
116 | |||
117 | /* bail if either operation raised an error */ | ||
118 | if(rc) | ||
119 | return rc; | ||
120 | if(rc2) | ||
121 | return rc2; | ||
122 | } | ||
123 | |||
124 | /* now flush to ensure all data was written */ | ||
125 | rc = xf_nandio_flush(nio); | ||
126 | if(rc) | ||
127 | return rc; | ||
128 | |||
129 | return XF_E_SUCCESS; | ||
130 | } | ||
131 | |||
132 | static const enum xf_nandio_mode update_mode_to_nandio[] = { | ||
133 | [XF_UPDATE] = XF_NANDIO_PROGRAM, | ||
134 | [XF_BACKUP] = XF_NANDIO_READ, | ||
135 | [XF_VERIFY] = XF_NANDIO_VERIFY, | ||
136 | }; | ||
137 | |||
138 | int xf_updater_run(enum xf_update_mode mode, struct xf_nandio* nio, | ||
139 | struct xf_map* map, int map_size, | ||
140 | xf_update_open_stream_cb open_stream, void* arg) | ||
141 | { | ||
142 | /* Switch NAND I/O into the correct mode */ | ||
143 | int rc = xf_nandio_set_mode(nio, update_mode_to_nandio[mode]); | ||
144 | if(rc) | ||
145 | return rc; | ||
146 | |||
147 | /* This does all the real work */ | ||
148 | return process_map(nio, map, map_size, open_stream, arg); | ||
149 | } | ||