blob: 6bd044e692ae11283931188acc487d6f0c5787b6 [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;
Elyes HAOUASf09239c2020-06-27 06:58:32 +0200249 int i;
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);
Stefan Reinauera27fa812019-11-24 21:58:02 -0800288 } else {
289 FILE *f;
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700290
Stefan Reinauera27fa812019-11-24 21:58:02 -0800291 printf("\nFirmware update with file %s\n", filename);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700292
Stefan Reinauera27fa812019-11-24 21:58:02 -0800293 f = fopen(filename, "rb");
294 if (!f) {
295 perror(filename);
296 return 0;
297 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700298
Stefan Reinauera27fa812019-11-24 21:58:02 -0800299 fseek(f, 0, SEEK_END);
300 fsize = ftell(f);
301 if (fsize < 0) {
302 perror(filename);
303 fclose(f);
304 return 0;
305 }
306 fseek(f, 0, SEEK_SET);
307
308 fw = malloc(fsize);
309 if (!fw) {
310 printf("ERROR: out of memory.\n");
311 fclose(f);
312 return 0;
313 }
314 if (fread(fw, fsize, 1, f) != 1) {
315 perror(filename);
316 fclose(f);
317 free(fw);
318 return 0;
319 }
Patrick Georgi21bbdb92019-08-23 13:15:34 +0200320 fclose(f);
Patrick Georgi21bbdb92019-08-23 13:15:34 +0200321 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700322
Stefan Reinauerbd8e1d02019-11-28 15:48:51 -0800323 if (em100->hwversion == HWVERSION_EM100PRO_EARLY && (memcmp(fw, "em100pro", 8) != 0 ||
324 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
325 printf("ERROR: Not an EM100Pro (original) firmware file.\n");
326 free(fw);
327 return 0;
328 }
329
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800330 if (em100->hwversion == HWVERSION_EM100PRO && (memcmp(fw, "em100pro", 8) != 0 ||
331 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
Stefan Reinauerf580b192019-11-18 15:24:22 -0800332 printf("ERROR: Not an EM100Pro (original) firmware file.\n");
333 free(fw);
334 return 0;
335 }
336
337 if (em100->hwversion == HWVERSION_EM100PRO_G2 && (memcmp(fw, "EM100Pro-G2", 11) != 0 ||
338 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
339 printf("ERROR: Not an EM100Pro-G2 firmware file.\n");
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700340 free(fw);
341 return 0;
342 }
343
Stefan Reinauer826d6682015-10-10 08:33:41 +0000344 /* Find firmwares in the update file */
345 fpga_offset = get_le32(fw+0x38);
346 fpga_len = get_le32(fw+0x3c);
347 mcu_offset = get_le32(fw+0x40);
348 mcu_len = get_le32(fw+0x44);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700349
Stefan Reinauerb825c922015-10-10 09:05:58 +0000350 /* Extracting versions */
351 strncpy(mcu_version, (char *)fw + 0x14, MAX_VERSION_LENGTH);
352 mcu_version[MAX_VERSION_LENGTH] = '\0';
353 strncpy(fpga_version, (char *)fw + 0x1e, MAX_VERSION_LENGTH);
354 fpga_version[MAX_VERSION_LENGTH] = '\0';
355
Stefan Reinauerf580b192019-11-18 15:24:22 -0800356 printf("EM100Pro%s Update File: %s\n",
357 em100->hwversion == HWVERSION_EM100PRO_G2 ? "-G2" : "", filename);
Stefan Reinauerd3c3df22019-11-20 23:59:36 -0800358 if (em100->hwversion == HWVERSION_EM100PRO)
359 printf(" Installed version: MCU %d.%d, FPGA %d.%d (%s)\n",
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700360 em100->mcu >> 8, em100->mcu & 0xff,
361 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff,
362 em100->fpga & 0x8000 ? "1.8V" : "3.3V");
Stefan Reinauerd3c3df22019-11-20 23:59:36 -0800363 else
364 printf(" Installed version: MCU %d.%d, FPGA %d.%03d\n",
365 em100->mcu >> 8, em100->mcu & 0xff,
366 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff);
367
Stefan Reinauerb825c922015-10-10 09:05:58 +0000368 printf(" New version: MCU %s, FPGA %s\n",
369 mcu_version, fpga_version);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700370
Stefan Reinauerfba6eb32015-10-10 10:22:12 +0000371 if (fpga_len < 256 || mcu_len < 256 ||
372 fpga_len > 0x100000 || mcu_len > 0xf0000) {
Stefan Reinauer2d836162015-10-10 09:40:07 +0000373 printf("\nFirmware file not valid.\n");
374 free(fw);
375 return 0;
376 }
377
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700378 /* Unlock and erase sector. Reading
379 * the SPI flash ID is requires to
380 * actually unlock the chip.
381 */
382 unlock_spi_flash(em100);
383 get_spi_flash_id(em100);
384
385 printf("Erasing firmware:\n");
386 for (i=0; i<=0x1e; i++) {
387 print_progress(i * 100 / 0x1e);
388 erase_spi_flash_sector(em100, i);
389 }
390 get_spi_flash_id(em100); // Needed?
391
392 printf("Writing firmware:\n");
393
394 /* Writing FPGA firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000395 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700396 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000397 memcpy(page, fw + fpga_offset + i, fpga_len - i
398 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700399 write_spi_flash_page(em100, i, page);
400 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000401 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700402 }
403
404 /* Writing MCU firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000405 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700406 memset(page, 0xff, 256);
Stefan Reinauer1205b2c2015-10-10 13:38:16 +0000407 memcpy(page, fw + mcu_offset + i, (mcu_len - i)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000408 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700409 write_spi_flash_page(em100, i + 0x100100, page);
410 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000411 print_progress(((fpga_len + i) * 100) /
412 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700413 }
414 print_progress(100);
415
416 if (verify) {
417 printf("Verifying firmware:\n");
Stefan Reinauer826d6682015-10-10 08:33:41 +0000418 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700419 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000420 memcpy(page, fw + fpga_offset + i, fpga_len - i
421 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700422 read_spi_flash_page(em100, i, vpage);
423
424 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000425 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700426 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600427 printf("\nERROR: Could not write FPGA firmware"
428 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700429 }
Stefan Reinauer826d6682015-10-10 08:33:41 +0000430 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700431 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000432 memcpy(page, fw + mcu_offset + i, mcu_len - i
433 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700434 read_spi_flash_page(em100, i + 0x100100, vpage);
435
436 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000437 print_progress(((fpga_len + i) * 100) /
438 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700439 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600440 printf("\nERROR: Could not write MCU firmware"
441 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700442 }
443 }
444 print_progress(100);
445
446 free(fw);
447
Stefan Reinauerf580b192019-11-18 15:24:22 -0800448 /* Write magic update tag '.UBOOTU.' */
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700449 memset(page, 0x00, 256);
450 page[0] = 0xaa;
451 page[1] = 0x55;
452 page[2] = 0x42;
453 page[3] = 0x4f;
454 page[4] = 0x4f;
455 page[5] = 0x54;
456 page[6] = 0x55;
457 page[7] = 0xaa;
458 write_spi_flash_page(em100, 0x100000, page);
459
460 if (verify) {
461 read_spi_flash_page(em100, 0x100000, vpage);
462 if (memcmp(page, vpage, 256))
463 printf("ERROR: Could not write update tag.\n");
464 }
465
466 printf("\nDisconnect and reconnect your EM100pro\n");
467
468 return 1;
469}