blob: f86362c114ab12a3197308f95867719e614a74eb [file] [log] [blame]
Stefan Reinauera2b67d32015-09-01 16:30:43 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Stefan Reinauera2b67d32015-09-01 16:30:43 -070012 */
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include "em100.h"
19
Stefan Reinauerdc3ae492019-11-21 00:14:32 -080020/* Firmware Update File Format
21 * ===========================
Stefan Reinauera2b67d32015-09-01 16:30:43 -070022 *
Stefan Reinauerdc3ae492019-11-21 00:14:32 -080023 * 0x0000000: 65 6d 31 30 30 70 72 6f - magic (20 bytes)
24 * 0x0000014: 32 2e 32 36 - version MCU (10 bytes)
25 * 0x000001e: 30 2e 37 35 - version FPGA (10 bytes)
26 * 0x0000028: 57 46 50 44 - Rev / WFPD (16 bytes)
27 * 0x0000038: 00 01 00 00 - file offset FPGA (4 bytes)
28 * 0x000003c: 44 15 07 00 - file length FPGA (4 bytes)
29 * 0x0000040: 00 17 07 00 - file offset MCU (4 bytes)
30 * 0x0000044: 00 bf 00 00 - file length MCU (4 bytes)
Stefan Reinauera2b67d32015-09-01 16:30:43 -070031 *
Stefan Reinauerdc3ae492019-11-21 00:14:32 -080032 * EM100Pro / EM100Pro-G2 Flash Layout
33 * ===================================
34 *
Stefan Reinauera2b67d32015-09-01 16:30:43 -070035 * 0x0000000: fpga firmware
36 * 0x0100000: 256 bytes of 0x00 filled space
37 * 0x0100100: mcu firmware
38 * 0x01f0000: 4 bytes secret key, 00 padded
Stefan Reinauerdc3ae492019-11-21 00:14:32 -080039 * 0x01fff00: ff xx yy yy yy yy ff ff
Stefan Reinauerbd8e1d02019-11-28 15:48:51 -080040 * xx: HW version (0xff, 0x04, 0x06)
Stefan Reinauerdc3ae492019-11-21 00:14:32 -080041 * yy: 4 bytes serial number
Stefan Reinauera2b67d32015-09-01 16:30:43 -070042 *
Stefan Reinauerdc3ae492019-11-21 00:14:32 -080043 * Notes
44 * =====
45 *
46 * - Empty pages remain 0xff filled.
47 * - Partially used pages are typically filled up with 00 bytes
48 * except the page containing the serial number.
49 * - After a FPGA firmware update, the page at 0x0100000 needs to
50 * be written with 0xaa 0x55 BOOT 0x55 0xaa to make the FPGA
51 * slurp in the new firmware.
Stefan Reinauera2b67d32015-09-01 16:30:43 -070052 */
53
54#undef DEBUG
55
56static void print_progress(int percent)
57{
58 int i;
59
60 putchar('\r');
61 putchar('[');
62 for (i = 0; i < percent / 2; i++)
63 putchar('=');
64 for (i = 0; i < 50 - (percent / 2); i++)
65 putchar(' ');
66 putchar(']');
67 if (percent == 100)
68 putchar('\n');
69 fflush(stdout);
70}
71
72static uint32_t get_le32(const unsigned char *in)
73{
74 return (in[3] << 24) | (in[2] << 16) | (in[1] << 8) | (in[0] << 0);
75}
76
Stefan Reinauera9278a92015-09-01 16:16:27 -070077static void put_le32(unsigned char *out, uint32_t val)
78{
79 out[0] = val&0xff;
80 out[1] = (val>>8)&0xff;
81 out[2] = (val>>16)&0xff;
82 out[3] = (val>>24)&0xff;
83}
84
Martin Rothb54d6ba2015-09-29 14:49:37 -060085int firmware_dump(struct em100 *em100, const char *filename,
86 int firmware_is_dpfw)
Stefan Reinauera2b67d32015-09-01 16:30:43 -070087{
Stefan Reinauerf70ff252019-11-13 18:58:16 -080088 unsigned char *data;
Stefan Reinauer79533882019-11-22 00:57:29 -080089 size_t i, rom_size = 0;
90 uint32_t id;
Stefan Reinauera2b67d32015-09-01 16:30:43 -070091 FILE *fw;
92
Stefan Reinauerf70ff252019-11-13 18:58:16 -080093 id = get_spi_flash_id(em100);
94 switch (id) {
95 case 0x202015:
96 rom_size = 2 MB;
97 break;
98 default:
99 printf("Unknown SPI flash id = %06x. Please report\n", id);
100 return 1;
101 }
102 data = malloc(rom_size);
103 if (data == NULL) {
104 perror("Out of memory.\n");
105 exit(1);
106 }
107 memset(data, 0, rom_size);
108
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700109 printf("\nWriting EM100Pro firmware to file %s\n", filename);
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800110 for (i=0; i < rom_size; i+=256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700111 if((i & 0x7fff) == 0)
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800112 print_progress(i * 100 / rom_size);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700113 if (!read_spi_flash_page(em100, i, data+i)) {
114 if (!read_spi_flash_page(em100, i, data+i))
115 if (!read_spi_flash_page(em100, i, data+i))
Stefan Reinauer79533882019-11-22 00:57:29 -0800116 printf("\nERROR: Couldn't read @%08zx\n",
Martin Rothb54d6ba2015-09-29 14:49:37 -0600117 i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700118 }
119 }
120 print_progress(100);
121 fw = fopen(filename, "wb");
Stefan Reinauera9278a92015-09-01 16:16:27 -0700122 if (!fw) {
123 perror(filename);
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800124 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700125 return 0;
126 }
127
128 if (firmware_is_dpfw) {
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800129 int fpga_size = 0, mcu_size = 0, hdrversion = 0;
Stefan Reinauer9d603982017-11-20 12:40:05 -0800130 char all_ff[256];
Stefan Reinauera9278a92015-09-01 16:16:27 -0700131 char mcu_version[8];
132 char fpga_version[8];
133 unsigned char header[0x100];
134
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800135 switch (em100->hwversion) {
Stefan Reinauerbd8e1d02019-11-28 15:48:51 -0800136 case HWVERSION_EM100PRO_EARLY:
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800137 case HWVERSION_EM100PRO:
138 hdrversion=1;
139 break;
Stefan Reinauerf580b192019-11-18 15:24:22 -0800140 case HWVERSION_EM100PRO_G2:
141 hdrversion=2;
142 break;
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800143 default:
144 printf("Dumping DPFW firmware on hardware version %u is "
145 "not yet supported.\n", em100->hwversion);
146 exit(1);
147 }
148
Stefan Reinauer55730a82018-01-22 19:26:53 -0800149 memset(all_ff, 255, sizeof(all_ff));
Stefan Reinauera9278a92015-09-01 16:16:27 -0700150 for (i = 0; i < 0x100000; i+=0x100) {
151 if (memcmp(data+i, all_ff, 256) == 0)
152 break;
153 }
154 if (i == 0x100000) {
155 printf("Can't parse device firmware. Please extract"
156 " raw firmware instead.\n");
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800157 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700158 exit(1);
159 }
160 fpga_size = i;
161
162 for (i = 0; i < 0xfff00; i+=0x100) {
163 if (memcmp(data+0x100100+i, all_ff, 256) == 0)
164 break;
165 }
166 if (i == 0xfff00) {
167 printf("Can't parse device firmware. Please extract"
168 " raw firmware instead.\n");
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800169 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700170 exit(1);
171 }
172 mcu_size = i;
173
174 snprintf(mcu_version, 8, "%d.%d",
175 em100->mcu >> 8, em100->mcu & 0xff);
176 snprintf(fpga_version, 8, "%d.%d",
177 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff);
178
179 memset(header, 0, 0x100);
Stefan Reinauerf580b192019-11-18 15:24:22 -0800180 switch (hdrversion) {
181 case 1:
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800182 memcpy(header, "em100pro", 8);
Stefan Reinauerf580b192019-11-18 15:24:22 -0800183 break;
184 case 2:
185 memcpy(header, "EM100Pro-G2", 11);
186 break;
187 }
Stefan Reinauera9278a92015-09-01 16:16:27 -0700188 memcpy(header + 0x28, "WFPD", 4);
189 memcpy(header + 0x14, mcu_version, 4);
190 memcpy(header + 0x1e, fpga_version, 4);
191 put_le32(header + 0x38, 0x100);
192 put_le32(header + 0x3c, fpga_size);
193 put_le32(header + 0x40, 0x100 + fpga_size);
194 put_le32(header + 0x44, 0x100 + mcu_size);
195 fwrite(header, 0x100, 1, fw);
196 fwrite(data, fpga_size, 1, fw);
197 fwrite(data + 0x100100, mcu_size, 1, fw);
198 } else {
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800199 if (fwrite(data, rom_size, 1, fw) != 1)
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700200 printf("ERROR: Couldn't write %s\n", filename);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700201 }
Stefan Reinauera9278a92015-09-01 16:16:27 -0700202 fclose(fw);
203
Stefan Reinauer6a5ed5b2019-11-19 14:52:16 -0800204#ifdef DEBUG
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800205 hexdump(data, rom_size);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700206#endif
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800207 free(data);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700208 return 0;
209}
210
Stefan Reinauera27fa812019-11-24 21:58:02 -0800211typedef struct {
212 TFILE *autoupdate_file;
213 struct em100 *em100;
214} firmware_update_t;
215
216static int firmware_update_entry(char *name, TFILE *file, void *data, int ok __unused)
217{
218 firmware_update_t *update = (firmware_update_t *)data;
219
220 /* firmware files are sorted oldest to newest */
221 /* filenames look like firmware/em100pro_fw_2.27_0.89_3.3V.dpfw */
222 if (update->em100->hwversion == HWVERSION_EM100PRO_EARLY ||
223 update->em100->hwversion == HWVERSION_EM100PRO) {
224#define FIRMWARE_PATH "firmware/em100pro_fw_"
225 if (strncmp(name, FIRMWARE_PATH, strlen(FIRMWARE_PATH)))
226 return 0;
227 /* The last file that matches is therefore the newest */
228
229 if ((update->em100->fpga & 0x8000 &&
230 strstr(name, "1.8V")) || strstr(name, "3.3V")) {
231 update->autoupdate_file=file;
232 printf("select %s\n", name);
233 }
234 return 0;
235 }
236
237 /* hwversion 6 */
238 printf("EM100Pro-G2 currently does not support auto-updating firmware.\n");
239
240 return 1;
241}
242
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700243int firmware_update(struct em100 *em100, const char *filename, int verify)
244{
Stefan Reinauerb825c922015-10-10 09:05:58 +0000245#define MAX_VERSION_LENGTH 10
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700246 unsigned char page[256], vpage[256];
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700247 long fsize;
248 unsigned char *fw;
Stefan Reinauera27fa812019-11-24 21:58:02 -0800249 int i, automatic = 0;
Stefan Reinauer826d6682015-10-10 08:33:41 +0000250 int fpga_offset, fpga_len, mcu_offset, mcu_len;
Stefan Reinauerb825c922015-10-10 09:05:58 +0000251 char fpga_version[MAX_VERSION_LENGTH + 1],
252 mcu_version[MAX_VERSION_LENGTH + 1];
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700253
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800254 switch (em100->hwversion) {
Stefan Reinauerbd8e1d02019-11-28 15:48:51 -0800255 case HWVERSION_EM100PRO_EARLY:
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800256 case HWVERSION_EM100PRO:
Stefan Reinauerf580b192019-11-18 15:24:22 -0800257 printf("Detected EM100Pro (original).\n");
258 break;
259 case HWVERSION_EM100PRO_G2:
260 printf("Detected EM100Pro-G2.\n");
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800261 break;
262 default:
263 printf("Updating EM100Pro firmware on hardware version %u is "
264 "not yet supported.\n", em100->hwversion);
265 exit(1);
266 }
267
Stefan Reinauera27fa812019-11-24 21:58:02 -0800268 if (!strncasecmp(filename, "auto", 5)) {
269 printf("\nAutomatic firmware update.\n");
Patrick Georgi13593772020-04-23 09:22:33 +0200270 char *firmware_bundle = get_em100_file("firmware.tar.xz");
271 TFILE *f = tar_load_compressed(firmware_bundle);
272 free(firmware_bundle);
Stefan Reinauera27fa812019-11-24 21:58:02 -0800273 firmware_update_t data;
274 data.em100 = em100;
275 data.autoupdate_file = NULL;
276 tar_for_each(f, firmware_update_entry, (void *)&data);
277 if (data.autoupdate_file == NULL) {
278 printf("Could not find suitable firmware for autoupdate\n");
279 return 0;
280 }
281 fsize = data.autoupdate_file->length;
282 fw = malloc(fsize);
283 if (!fw) {
284 printf("ERROR: out of memory.\n");
285 return 0;
286 }
287 memcpy(fw, data.autoupdate_file->address, fsize);
288 automatic = 1;
289 } else {
290 FILE *f;
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700291
Stefan Reinauera27fa812019-11-24 21:58:02 -0800292 printf("\nFirmware update with file %s\n", filename);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700293
Stefan Reinauera27fa812019-11-24 21:58:02 -0800294 f = fopen(filename, "rb");
295 if (!f) {
296 perror(filename);
297 return 0;
298 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700299
Stefan Reinauera27fa812019-11-24 21:58:02 -0800300 fseek(f, 0, SEEK_END);
301 fsize = ftell(f);
302 if (fsize < 0) {
303 perror(filename);
304 fclose(f);
305 return 0;
306 }
307 fseek(f, 0, SEEK_SET);
308
309 fw = malloc(fsize);
310 if (!fw) {
311 printf("ERROR: out of memory.\n");
312 fclose(f);
313 return 0;
314 }
315 if (fread(fw, fsize, 1, f) != 1) {
316 perror(filename);
317 fclose(f);
318 free(fw);
319 return 0;
320 }
Patrick Georgi21bbdb92019-08-23 13:15:34 +0200321 fclose(f);
Patrick Georgi21bbdb92019-08-23 13:15:34 +0200322 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700323
Stefan Reinauerbd8e1d02019-11-28 15:48:51 -0800324 if (em100->hwversion == HWVERSION_EM100PRO_EARLY && (memcmp(fw, "em100pro", 8) != 0 ||
325 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
326 printf("ERROR: Not an EM100Pro (original) firmware file.\n");
327 free(fw);
328 return 0;
329 }
330
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800331 if (em100->hwversion == HWVERSION_EM100PRO && (memcmp(fw, "em100pro", 8) != 0 ||
332 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
Stefan Reinauerf580b192019-11-18 15:24:22 -0800333 printf("ERROR: Not an EM100Pro (original) firmware file.\n");
334 free(fw);
335 return 0;
336 }
337
338 if (em100->hwversion == HWVERSION_EM100PRO_G2 && (memcmp(fw, "EM100Pro-G2", 11) != 0 ||
339 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
340 printf("ERROR: Not an EM100Pro-G2 firmware file.\n");
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700341 free(fw);
342 return 0;
343 }
344
Stefan Reinauer826d6682015-10-10 08:33:41 +0000345 /* Find firmwares in the update file */
346 fpga_offset = get_le32(fw+0x38);
347 fpga_len = get_le32(fw+0x3c);
348 mcu_offset = get_le32(fw+0x40);
349 mcu_len = get_le32(fw+0x44);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700350
Stefan Reinauerb825c922015-10-10 09:05:58 +0000351 /* Extracting versions */
352 strncpy(mcu_version, (char *)fw + 0x14, MAX_VERSION_LENGTH);
353 mcu_version[MAX_VERSION_LENGTH] = '\0';
354 strncpy(fpga_version, (char *)fw + 0x1e, MAX_VERSION_LENGTH);
355 fpga_version[MAX_VERSION_LENGTH] = '\0';
356
Stefan Reinauerf580b192019-11-18 15:24:22 -0800357 printf("EM100Pro%s Update File: %s\n",
358 em100->hwversion == HWVERSION_EM100PRO_G2 ? "-G2" : "", filename);
Stefan Reinauerd3c3df22019-11-20 23:59:36 -0800359 if (em100->hwversion == HWVERSION_EM100PRO)
360 printf(" Installed version: MCU %d.%d, FPGA %d.%d (%s)\n",
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700361 em100->mcu >> 8, em100->mcu & 0xff,
362 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff,
363 em100->fpga & 0x8000 ? "1.8V" : "3.3V");
Stefan Reinauerd3c3df22019-11-20 23:59:36 -0800364 else
365 printf(" Installed version: MCU %d.%d, FPGA %d.%03d\n",
366 em100->mcu >> 8, em100->mcu & 0xff,
367 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff);
368
Stefan Reinauerb825c922015-10-10 09:05:58 +0000369 printf(" New version: MCU %s, FPGA %s\n",
370 mcu_version, fpga_version);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700371
Stefan Reinauerfba6eb32015-10-10 10:22:12 +0000372 if (fpga_len < 256 || mcu_len < 256 ||
373 fpga_len > 0x100000 || mcu_len > 0xf0000) {
Stefan Reinauer2d836162015-10-10 09:40:07 +0000374 printf("\nFirmware file not valid.\n");
375 free(fw);
376 return 0;
377 }
378
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700379 /* Unlock and erase sector. Reading
380 * the SPI flash ID is requires to
381 * actually unlock the chip.
382 */
383 unlock_spi_flash(em100);
384 get_spi_flash_id(em100);
385
386 printf("Erasing firmware:\n");
387 for (i=0; i<=0x1e; i++) {
388 print_progress(i * 100 / 0x1e);
389 erase_spi_flash_sector(em100, i);
390 }
391 get_spi_flash_id(em100); // Needed?
392
393 printf("Writing firmware:\n");
394
395 /* Writing FPGA firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000396 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700397 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000398 memcpy(page, fw + fpga_offset + i, fpga_len - i
399 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700400 write_spi_flash_page(em100, i, page);
401 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000402 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700403 }
404
405 /* Writing MCU firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000406 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700407 memset(page, 0xff, 256);
Stefan Reinauer1205b2c2015-10-10 13:38:16 +0000408 memcpy(page, fw + mcu_offset + i, (mcu_len - i)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000409 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700410 write_spi_flash_page(em100, i + 0x100100, page);
411 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000412 print_progress(((fpga_len + i) * 100) /
413 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700414 }
415 print_progress(100);
416
417 if (verify) {
418 printf("Verifying firmware:\n");
Stefan Reinauer826d6682015-10-10 08:33:41 +0000419 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700420 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000421 memcpy(page, fw + fpga_offset + i, fpga_len - i
422 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700423 read_spi_flash_page(em100, i, vpage);
424
425 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000426 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700427 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600428 printf("\nERROR: Could not write FPGA firmware"
429 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700430 }
Stefan Reinauer826d6682015-10-10 08:33:41 +0000431 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700432 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000433 memcpy(page, fw + mcu_offset + i, mcu_len - i
434 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700435 read_spi_flash_page(em100, i + 0x100100, vpage);
436
437 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000438 print_progress(((fpga_len + i) * 100) /
439 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700440 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600441 printf("\nERROR: Could not write MCU firmware"
442 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700443 }
444 }
445 print_progress(100);
446
447 free(fw);
448
Stefan Reinauerf580b192019-11-18 15:24:22 -0800449 /* Write magic update tag '.UBOOTU.' */
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700450 memset(page, 0x00, 256);
451 page[0] = 0xaa;
452 page[1] = 0x55;
453 page[2] = 0x42;
454 page[3] = 0x4f;
455 page[4] = 0x4f;
456 page[5] = 0x54;
457 page[6] = 0x55;
458 page[7] = 0xaa;
459 write_spi_flash_page(em100, 0x100000, page);
460
461 if (verify) {
462 read_spi_flash_page(em100, 0x100000, vpage);
463 if (memcmp(page, vpage, 256))
464 printf("ERROR: Could not write update tag.\n");
465 }
466
467 printf("\nDisconnect and reconnect your EM100pro\n");
468
469 return 1;
470}