blob: 384e3f30b4bce3f0ef554c4701a0ef10af3214d7 [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
211int firmware_update(struct em100 *em100, const char *filename, int verify)
212{
Stefan Reinauerb825c922015-10-10 09:05:58 +0000213#define MAX_VERSION_LENGTH 10
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700214 unsigned char page[256], vpage[256];
215 FILE *f;
216 long fsize;
217 unsigned char *fw;
218 int i;
Stefan Reinauer826d6682015-10-10 08:33:41 +0000219 int fpga_offset, fpga_len, mcu_offset, mcu_len;
Stefan Reinauerb825c922015-10-10 09:05:58 +0000220 char fpga_version[MAX_VERSION_LENGTH + 1],
221 mcu_version[MAX_VERSION_LENGTH + 1];
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700222
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800223 switch (em100->hwversion) {
Stefan Reinauerbd8e1d02019-11-28 15:48:51 -0800224 case HWVERSION_EM100PRO_EARLY:
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800225 case HWVERSION_EM100PRO:
Stefan Reinauerf580b192019-11-18 15:24:22 -0800226 printf("Detected EM100Pro (original).\n");
227 break;
228 case HWVERSION_EM100PRO_G2:
229 printf("Detected EM100Pro-G2.\n");
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800230 break;
231 default:
232 printf("Updating EM100Pro firmware on hardware version %u is "
233 "not yet supported.\n", em100->hwversion);
234 exit(1);
235 }
236
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700237 printf("\nAttempting firmware update with file %s\n", filename);
238
239 f = fopen(filename, "rb");
240 if (!f) {
241 perror(filename);
242 return 0;
243 }
244
245 fseek(f, 0, SEEK_END);
246 fsize = ftell(f);
Stefan Reinauer85c22ea2015-10-09 13:53:56 +0000247 if (fsize < 0) {
248 perror(filename);
Stefan Reinauer95b67a02015-10-10 10:02:02 +0000249 fclose(f);
Stefan Reinauer85c22ea2015-10-09 13:53:56 +0000250 return 0;
251 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700252 fseek(f, 0, SEEK_SET);
253
254 fw = malloc(fsize);
Patrick Georgi21bbdb92019-08-23 13:15:34 +0200255 if (!fw) {
256 printf("ERROR: out of memory.\n");
257 fclose(f);
258 return 0;
259 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700260 if (fread(fw, fsize, 1, f) != 1) {
261 perror(filename);
262 fclose(f);
Stefan Reinauerb02440f2015-10-10 08:45:30 +0000263 free(fw);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700264 return 0;
265 }
266 fclose(f);
267
Stefan Reinauerbd8e1d02019-11-28 15:48:51 -0800268 if (em100->hwversion == HWVERSION_EM100PRO_EARLY && (memcmp(fw, "em100pro", 8) != 0 ||
269 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
270 printf("ERROR: Not an EM100Pro (original) firmware file.\n");
271 free(fw);
272 return 0;
273 }
274
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800275 if (em100->hwversion == HWVERSION_EM100PRO && (memcmp(fw, "em100pro", 8) != 0 ||
276 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
Stefan Reinauerf580b192019-11-18 15:24:22 -0800277 printf("ERROR: Not an EM100Pro (original) firmware file.\n");
278 free(fw);
279 return 0;
280 }
281
282 if (em100->hwversion == HWVERSION_EM100PRO_G2 && (memcmp(fw, "EM100Pro-G2", 11) != 0 ||
283 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
284 printf("ERROR: Not an EM100Pro-G2 firmware file.\n");
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700285 free(fw);
286 return 0;
287 }
288
Stefan Reinauer826d6682015-10-10 08:33:41 +0000289 /* Find firmwares in the update file */
290 fpga_offset = get_le32(fw+0x38);
291 fpga_len = get_le32(fw+0x3c);
292 mcu_offset = get_le32(fw+0x40);
293 mcu_len = get_le32(fw+0x44);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700294
Stefan Reinauerb825c922015-10-10 09:05:58 +0000295 /* Extracting versions */
296 strncpy(mcu_version, (char *)fw + 0x14, MAX_VERSION_LENGTH);
297 mcu_version[MAX_VERSION_LENGTH] = '\0';
298 strncpy(fpga_version, (char *)fw + 0x1e, MAX_VERSION_LENGTH);
299 fpga_version[MAX_VERSION_LENGTH] = '\0';
300
Stefan Reinauerf580b192019-11-18 15:24:22 -0800301 printf("EM100Pro%s Update File: %s\n",
302 em100->hwversion == HWVERSION_EM100PRO_G2 ? "-G2" : "", filename);
Stefan Reinauerd3c3df22019-11-20 23:59:36 -0800303 if (em100->hwversion == HWVERSION_EM100PRO)
304 printf(" Installed version: MCU %d.%d, FPGA %d.%d (%s)\n",
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700305 em100->mcu >> 8, em100->mcu & 0xff,
306 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff,
307 em100->fpga & 0x8000 ? "1.8V" : "3.3V");
Stefan Reinauerd3c3df22019-11-20 23:59:36 -0800308 else
309 printf(" Installed version: MCU %d.%d, FPGA %d.%03d\n",
310 em100->mcu >> 8, em100->mcu & 0xff,
311 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff);
312
Stefan Reinauerb825c922015-10-10 09:05:58 +0000313 printf(" New version: MCU %s, FPGA %s\n",
314 mcu_version, fpga_version);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700315
Stefan Reinauerfba6eb32015-10-10 10:22:12 +0000316 if (fpga_len < 256 || mcu_len < 256 ||
317 fpga_len > 0x100000 || mcu_len > 0xf0000) {
Stefan Reinauer2d836162015-10-10 09:40:07 +0000318 printf("\nFirmware file not valid.\n");
319 free(fw);
320 return 0;
321 }
322
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700323 /* Unlock and erase sector. Reading
324 * the SPI flash ID is requires to
325 * actually unlock the chip.
326 */
327 unlock_spi_flash(em100);
328 get_spi_flash_id(em100);
329
330 printf("Erasing firmware:\n");
331 for (i=0; i<=0x1e; i++) {
332 print_progress(i * 100 / 0x1e);
333 erase_spi_flash_sector(em100, i);
334 }
335 get_spi_flash_id(em100); // Needed?
336
337 printf("Writing firmware:\n");
338
339 /* Writing FPGA firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000340 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700341 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000342 memcpy(page, fw + fpga_offset + i, fpga_len - i
343 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700344 write_spi_flash_page(em100, i, page);
345 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000346 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700347 }
348
349 /* Writing MCU firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000350 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700351 memset(page, 0xff, 256);
Stefan Reinauer1205b2c2015-10-10 13:38:16 +0000352 memcpy(page, fw + mcu_offset + i, (mcu_len - i)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000353 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700354 write_spi_flash_page(em100, i + 0x100100, page);
355 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000356 print_progress(((fpga_len + i) * 100) /
357 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700358 }
359 print_progress(100);
360
361 if (verify) {
362 printf("Verifying firmware:\n");
Stefan Reinauer826d6682015-10-10 08:33:41 +0000363 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700364 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000365 memcpy(page, fw + fpga_offset + i, fpga_len - i
366 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700367 read_spi_flash_page(em100, i, vpage);
368
369 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000370 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700371 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600372 printf("\nERROR: Could not write FPGA firmware"
373 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700374 }
Stefan Reinauer826d6682015-10-10 08:33:41 +0000375 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700376 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000377 memcpy(page, fw + mcu_offset + i, mcu_len - i
378 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700379 read_spi_flash_page(em100, i + 0x100100, vpage);
380
381 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000382 print_progress(((fpga_len + i) * 100) /
383 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700384 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600385 printf("\nERROR: Could not write MCU firmware"
386 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700387 }
388 }
389 print_progress(100);
390
391 free(fw);
392
Stefan Reinauerf580b192019-11-18 15:24:22 -0800393 /* Write magic update tag '.UBOOTU.' */
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700394 memset(page, 0x00, 256);
395 page[0] = 0xaa;
396 page[1] = 0x55;
397 page[2] = 0x42;
398 page[3] = 0x4f;
399 page[4] = 0x4f;
400 page[5] = 0x54;
401 page[6] = 0x55;
402 page[7] = 0xaa;
403 write_spi_flash_page(em100, 0x100000, page);
404
405 if (verify) {
406 read_spi_flash_page(em100, 0x100000, vpage);
407 if (memcmp(page, vpage, 256))
408 printf("ERROR: Could not write update tag.\n");
409 }
410
411 printf("\nDisconnect and reconnect your EM100pro\n");
412
413 return 1;
414}