blob: b468dc13d84a7064b5cd9fe6394d752d87662530 [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.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
Martin Rothb54d6ba2015-09-29 14:49:37 -060015 * Foundation, Inc.
Stefan Reinauera2b67d32015-09-01 16:30:43 -070016 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include "em100.h"
23
Stefan Reinauerdc3ae492019-11-21 00:14:32 -080024/* Firmware Update File Format
25 * ===========================
Stefan Reinauera2b67d32015-09-01 16:30:43 -070026 *
Stefan Reinauerdc3ae492019-11-21 00:14:32 -080027 * 0x0000000: 65 6d 31 30 30 70 72 6f - magic (20 bytes)
28 * 0x0000014: 32 2e 32 36 - version MCU (10 bytes)
29 * 0x000001e: 30 2e 37 35 - version FPGA (10 bytes)
30 * 0x0000028: 57 46 50 44 - Rev / WFPD (16 bytes)
31 * 0x0000038: 00 01 00 00 - file offset FPGA (4 bytes)
32 * 0x000003c: 44 15 07 00 - file length FPGA (4 bytes)
33 * 0x0000040: 00 17 07 00 - file offset MCU (4 bytes)
34 * 0x0000044: 00 bf 00 00 - file length MCU (4 bytes)
Stefan Reinauera2b67d32015-09-01 16:30:43 -070035 *
Stefan Reinauerdc3ae492019-11-21 00:14:32 -080036 * EM100Pro / EM100Pro-G2 Flash Layout
37 * ===================================
38 *
Stefan Reinauera2b67d32015-09-01 16:30:43 -070039 * 0x0000000: fpga firmware
40 * 0x0100000: 256 bytes of 0x00 filled space
41 * 0x0100100: mcu firmware
42 * 0x01f0000: 4 bytes secret key, 00 padded
Stefan Reinauerdc3ae492019-11-21 00:14:32 -080043 * 0x01fff00: ff xx yy yy yy yy ff ff
44 * xx: HW version
45 * yy: 4 bytes serial number
Stefan Reinauera2b67d32015-09-01 16:30:43 -070046 *
Stefan Reinauerdc3ae492019-11-21 00:14:32 -080047 * Notes
48 * =====
49 *
50 * - Empty pages remain 0xff filled.
51 * - Partially used pages are typically filled up with 00 bytes
52 * except the page containing the serial number.
53 * - After a FPGA firmware update, the page at 0x0100000 needs to
54 * be written with 0xaa 0x55 BOOT 0x55 0xaa to make the FPGA
55 * slurp in the new firmware.
Stefan Reinauera2b67d32015-09-01 16:30:43 -070056 */
57
58#undef DEBUG
59
60static void print_progress(int percent)
61{
62 int i;
63
64 putchar('\r');
65 putchar('[');
66 for (i = 0; i < percent / 2; i++)
67 putchar('=');
68 for (i = 0; i < 50 - (percent / 2); i++)
69 putchar(' ');
70 putchar(']');
71 if (percent == 100)
72 putchar('\n');
73 fflush(stdout);
74}
75
76static uint32_t get_le32(const unsigned char *in)
77{
78 return (in[3] << 24) | (in[2] << 16) | (in[1] << 8) | (in[0] << 0);
79}
80
Stefan Reinauera9278a92015-09-01 16:16:27 -070081static void put_le32(unsigned char *out, uint32_t val)
82{
83 out[0] = val&0xff;
84 out[1] = (val>>8)&0xff;
85 out[2] = (val>>16)&0xff;
86 out[3] = (val>>24)&0xff;
87}
88
Martin Rothb54d6ba2015-09-29 14:49:37 -060089int firmware_dump(struct em100 *em100, const char *filename,
90 int firmware_is_dpfw)
Stefan Reinauera2b67d32015-09-01 16:30:43 -070091{
Stefan Reinauerf70ff252019-11-13 18:58:16 -080092 unsigned char *data;
Stefan Reinauera2b67d32015-09-01 16:30:43 -070093 int i;
Stefan Reinauerf70ff252019-11-13 18:58:16 -080094 uint32_t id, rom_size = 0;
Stefan Reinauera2b67d32015-09-01 16:30:43 -070095 FILE *fw;
96
Stefan Reinauerf70ff252019-11-13 18:58:16 -080097 id = get_spi_flash_id(em100);
98 switch (id) {
99 case 0x202015:
100 rom_size = 2 MB;
101 break;
102 default:
103 printf("Unknown SPI flash id = %06x. Please report\n", id);
104 return 1;
105 }
106 data = malloc(rom_size);
107 if (data == NULL) {
108 perror("Out of memory.\n");
109 exit(1);
110 }
111 memset(data, 0, rom_size);
112
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700113 printf("\nWriting EM100Pro firmware to file %s\n", filename);
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800114 for (i=0; i < rom_size; i+=256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700115 if((i & 0x7fff) == 0)
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800116 print_progress(i * 100 / rom_size);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700117 if (!read_spi_flash_page(em100, i, data+i)) {
118 if (!read_spi_flash_page(em100, i, data+i))
119 if (!read_spi_flash_page(em100, i, data+i))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600120 printf("\nERROR: Couldn't read @%08x\n",
121 i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700122 }
123 }
124 print_progress(100);
125 fw = fopen(filename, "wb");
Stefan Reinauera9278a92015-09-01 16:16:27 -0700126 if (!fw) {
127 perror(filename);
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800128 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700129 return 0;
130 }
131
132 if (firmware_is_dpfw) {
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800133 int fpga_size = 0, mcu_size = 0, hdrversion = 0;
Stefan Reinauer9d603982017-11-20 12:40:05 -0800134 char all_ff[256];
Stefan Reinauera9278a92015-09-01 16:16:27 -0700135 char mcu_version[8];
136 char fpga_version[8];
137 unsigned char header[0x100];
138
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800139 switch (em100->hwversion) {
140 case HWVERSION_EM100PRO:
141 hdrversion=1;
142 break;
Stefan Reinauerf580b192019-11-18 15:24:22 -0800143 case HWVERSION_EM100PRO_G2:
144 hdrversion=2;
145 break;
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800146 default:
147 printf("Dumping DPFW firmware on hardware version %u is "
148 "not yet supported.\n", em100->hwversion);
149 exit(1);
150 }
151
Stefan Reinauer55730a82018-01-22 19:26:53 -0800152 memset(all_ff, 255, sizeof(all_ff));
Stefan Reinauera9278a92015-09-01 16:16:27 -0700153 for (i = 0; i < 0x100000; i+=0x100) {
154 if (memcmp(data+i, all_ff, 256) == 0)
155 break;
156 }
157 if (i == 0x100000) {
158 printf("Can't parse device firmware. Please extract"
159 " raw firmware instead.\n");
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800160 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700161 exit(1);
162 }
163 fpga_size = i;
164
165 for (i = 0; i < 0xfff00; i+=0x100) {
166 if (memcmp(data+0x100100+i, all_ff, 256) == 0)
167 break;
168 }
169 if (i == 0xfff00) {
170 printf("Can't parse device firmware. Please extract"
171 " raw firmware instead.\n");
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800172 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700173 exit(1);
174 }
175 mcu_size = i;
176
177 snprintf(mcu_version, 8, "%d.%d",
178 em100->mcu >> 8, em100->mcu & 0xff);
179 snprintf(fpga_version, 8, "%d.%d",
180 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff);
181
182 memset(header, 0, 0x100);
Stefan Reinauerf580b192019-11-18 15:24:22 -0800183 switch (hdrversion) {
184 case 1:
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800185 memcpy(header, "em100pro", 8);
Stefan Reinauerf580b192019-11-18 15:24:22 -0800186 break;
187 case 2:
188 memcpy(header, "EM100Pro-G2", 11);
189 break;
190 }
Stefan Reinauera9278a92015-09-01 16:16:27 -0700191 memcpy(header + 0x28, "WFPD", 4);
192 memcpy(header + 0x14, mcu_version, 4);
193 memcpy(header + 0x1e, fpga_version, 4);
194 put_le32(header + 0x38, 0x100);
195 put_le32(header + 0x3c, fpga_size);
196 put_le32(header + 0x40, 0x100 + fpga_size);
197 put_le32(header + 0x44, 0x100 + mcu_size);
198 fwrite(header, 0x100, 1, fw);
199 fwrite(data, fpga_size, 1, fw);
200 fwrite(data + 0x100100, mcu_size, 1, fw);
201 } else {
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800202 if (fwrite(data, rom_size, 1, fw) != 1)
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700203 printf("ERROR: Couldn't write %s\n", filename);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700204 }
Stefan Reinauera9278a92015-09-01 16:16:27 -0700205 fclose(fw);
206
Stefan Reinauer6a5ed5b2019-11-19 14:52:16 -0800207#ifdef DEBUG
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800208 hexdump(data, rom_size);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700209#endif
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800210 free(data);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700211 return 0;
212}
213
214int firmware_update(struct em100 *em100, const char *filename, int verify)
215{
Stefan Reinauerb825c922015-10-10 09:05:58 +0000216#define MAX_VERSION_LENGTH 10
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700217 unsigned char page[256], vpage[256];
218 FILE *f;
219 long fsize;
220 unsigned char *fw;
221 int i;
Stefan Reinauer826d6682015-10-10 08:33:41 +0000222 int fpga_offset, fpga_len, mcu_offset, mcu_len;
Stefan Reinauerb825c922015-10-10 09:05:58 +0000223 char fpga_version[MAX_VERSION_LENGTH + 1],
224 mcu_version[MAX_VERSION_LENGTH + 1];
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700225
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800226 switch (em100->hwversion) {
227 case HWVERSION_EM100PRO:
Stefan Reinauerf580b192019-11-18 15:24:22 -0800228 printf("Detected EM100Pro (original).\n");
229 break;
230 case HWVERSION_EM100PRO_G2:
231 printf("Detected EM100Pro-G2.\n");
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800232 break;
233 default:
234 printf("Updating EM100Pro firmware on hardware version %u is "
235 "not yet supported.\n", em100->hwversion);
236 exit(1);
237 }
238
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700239 printf("\nAttempting firmware update with file %s\n", filename);
240
241 f = fopen(filename, "rb");
242 if (!f) {
243 perror(filename);
244 return 0;
245 }
246
247 fseek(f, 0, SEEK_END);
248 fsize = ftell(f);
Stefan Reinauer85c22ea2015-10-09 13:53:56 +0000249 if (fsize < 0) {
250 perror(filename);
Stefan Reinauer95b67a02015-10-10 10:02:02 +0000251 fclose(f);
Stefan Reinauer85c22ea2015-10-09 13:53:56 +0000252 return 0;
253 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700254 fseek(f, 0, SEEK_SET);
255
256 fw = malloc(fsize);
Patrick Georgi21bbdb92019-08-23 13:15:34 +0200257 if (!fw) {
258 printf("ERROR: out of memory.\n");
259 fclose(f);
260 return 0;
261 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700262 if (fread(fw, fsize, 1, f) != 1) {
263 perror(filename);
264 fclose(f);
Stefan Reinauerb02440f2015-10-10 08:45:30 +0000265 free(fw);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700266 return 0;
267 }
268 fclose(f);
269
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800270 if (em100->hwversion == HWVERSION_EM100PRO && (memcmp(fw, "em100pro", 8) != 0 ||
271 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
Stefan Reinauerf580b192019-11-18 15:24:22 -0800272 printf("ERROR: Not an EM100Pro (original) firmware file.\n");
273 free(fw);
274 return 0;
275 }
276
277 if (em100->hwversion == HWVERSION_EM100PRO_G2 && (memcmp(fw, "EM100Pro-G2", 11) != 0 ||
278 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
279 printf("ERROR: Not an EM100Pro-G2 firmware file.\n");
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700280 free(fw);
281 return 0;
282 }
283
Stefan Reinauer826d6682015-10-10 08:33:41 +0000284 /* Find firmwares in the update file */
285 fpga_offset = get_le32(fw+0x38);
286 fpga_len = get_le32(fw+0x3c);
287 mcu_offset = get_le32(fw+0x40);
288 mcu_len = get_le32(fw+0x44);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700289
Stefan Reinauerb825c922015-10-10 09:05:58 +0000290 /* Extracting versions */
291 strncpy(mcu_version, (char *)fw + 0x14, MAX_VERSION_LENGTH);
292 mcu_version[MAX_VERSION_LENGTH] = '\0';
293 strncpy(fpga_version, (char *)fw + 0x1e, MAX_VERSION_LENGTH);
294 fpga_version[MAX_VERSION_LENGTH] = '\0';
295
Stefan Reinauerf580b192019-11-18 15:24:22 -0800296 printf("EM100Pro%s Update File: %s\n",
297 em100->hwversion == HWVERSION_EM100PRO_G2 ? "-G2" : "", filename);
Stefan Reinauerd3c3df22019-11-20 23:59:36 -0800298 if (em100->hwversion == HWVERSION_EM100PRO)
299 printf(" Installed version: MCU %d.%d, FPGA %d.%d (%s)\n",
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700300 em100->mcu >> 8, em100->mcu & 0xff,
301 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff,
302 em100->fpga & 0x8000 ? "1.8V" : "3.3V");
Stefan Reinauerd3c3df22019-11-20 23:59:36 -0800303 else
304 printf(" Installed version: MCU %d.%d, FPGA %d.%03d\n",
305 em100->mcu >> 8, em100->mcu & 0xff,
306 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff);
307
Stefan Reinauerb825c922015-10-10 09:05:58 +0000308 printf(" New version: MCU %s, FPGA %s\n",
309 mcu_version, fpga_version);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700310
Stefan Reinauerfba6eb32015-10-10 10:22:12 +0000311 if (fpga_len < 256 || mcu_len < 256 ||
312 fpga_len > 0x100000 || mcu_len > 0xf0000) {
Stefan Reinauer2d836162015-10-10 09:40:07 +0000313 printf("\nFirmware file not valid.\n");
314 free(fw);
315 return 0;
316 }
317
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700318 /* Unlock and erase sector. Reading
319 * the SPI flash ID is requires to
320 * actually unlock the chip.
321 */
322 unlock_spi_flash(em100);
323 get_spi_flash_id(em100);
324
325 printf("Erasing firmware:\n");
326 for (i=0; i<=0x1e; i++) {
327 print_progress(i * 100 / 0x1e);
328 erase_spi_flash_sector(em100, i);
329 }
330 get_spi_flash_id(em100); // Needed?
331
332 printf("Writing firmware:\n");
333
334 /* Writing FPGA firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000335 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700336 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000337 memcpy(page, fw + fpga_offset + i, fpga_len - i
338 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700339 write_spi_flash_page(em100, i, page);
340 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000341 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700342 }
343
344 /* Writing MCU firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000345 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700346 memset(page, 0xff, 256);
Stefan Reinauer1205b2c2015-10-10 13:38:16 +0000347 memcpy(page, fw + mcu_offset + i, (mcu_len - i)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000348 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700349 write_spi_flash_page(em100, i + 0x100100, page);
350 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000351 print_progress(((fpga_len + i) * 100) /
352 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700353 }
354 print_progress(100);
355
356 if (verify) {
357 printf("Verifying firmware:\n");
Stefan Reinauer826d6682015-10-10 08:33:41 +0000358 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700359 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000360 memcpy(page, fw + fpga_offset + i, fpga_len - i
361 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700362 read_spi_flash_page(em100, i, vpage);
363
364 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000365 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700366 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600367 printf("\nERROR: Could not write FPGA firmware"
368 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700369 }
Stefan Reinauer826d6682015-10-10 08:33:41 +0000370 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700371 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000372 memcpy(page, fw + mcu_offset + i, mcu_len - i
373 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700374 read_spi_flash_page(em100, i + 0x100100, vpage);
375
376 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000377 print_progress(((fpga_len + i) * 100) /
378 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700379 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600380 printf("\nERROR: Could not write MCU firmware"
381 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700382 }
383 }
384 print_progress(100);
385
386 free(fw);
387
Stefan Reinauerf580b192019-11-18 15:24:22 -0800388 /* Write magic update tag '.UBOOTU.' */
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700389 memset(page, 0x00, 256);
390 page[0] = 0xaa;
391 page[1] = 0x55;
392 page[2] = 0x42;
393 page[3] = 0x4f;
394 page[4] = 0x4f;
395 page[5] = 0x54;
396 page[6] = 0x55;
397 page[7] = 0xaa;
398 write_spi_flash_page(em100, 0x100000, page);
399
400 if (verify) {
401 read_spi_flash_page(em100, 0x100000, vpage);
402 if (memcmp(page, vpage, 256))
403 printf("ERROR: Could not write update tag.\n");
404 }
405
406 printf("\nDisconnect and reconnect your EM100pro\n");
407
408 return 1;
409}