blob: 329b54367a1cda95990ef097b1767cf7d078c15f [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
24/* file format:
25 *
26 * 0x0000000: 65 6d 31 30 30 70 72 6f - magic 8 bytes
27 * 0x0000014: 32 2e 32 36 - version MCU
28 * 0x000001e: 30 2e 37 35 - version FPGA
29 * 0x0000028: 57 46 50 44 - WFPD (??)
30 * 0x0000038: 00 01 00 00 - file offset FPGA
31 * 0x000003c: 44 15 07 00 - file length FPGA
32 * 0x0000040: 00 17 07 00 - file offset MCU
33 * 0x0000044: 00 bf 00 00 - file length MCU
34 *
35 * flash layout
36 * 0x0000000: fpga firmware
37 * 0x0100000: 256 bytes of 0x00 filled space
38 * 0x0100100: mcu firmware
39 * 0x01f0000: 4 bytes secret key, 00 padded
40 * 0x01fff00: ff ff 4 bytes serial number ff padded
41 *
42 * - empty pages remain 0xff filled
43 * - partially used pages are typically filled up with 00 bytes
44 * except the page containing the serial number
45 */
46
47#undef DEBUG
48
49static void print_progress(int percent)
50{
51 int i;
52
53 putchar('\r');
54 putchar('[');
55 for (i = 0; i < percent / 2; i++)
56 putchar('=');
57 for (i = 0; i < 50 - (percent / 2); i++)
58 putchar(' ');
59 putchar(']');
60 if (percent == 100)
61 putchar('\n');
62 fflush(stdout);
63}
64
65static uint32_t get_le32(const unsigned char *in)
66{
67 return (in[3] << 24) | (in[2] << 16) | (in[1] << 8) | (in[0] << 0);
68}
69
Stefan Reinauera9278a92015-09-01 16:16:27 -070070static void put_le32(unsigned char *out, uint32_t val)
71{
72 out[0] = val&0xff;
73 out[1] = (val>>8)&0xff;
74 out[2] = (val>>16)&0xff;
75 out[3] = (val>>24)&0xff;
76}
77
Martin Rothb54d6ba2015-09-29 14:49:37 -060078int firmware_dump(struct em100 *em100, const char *filename,
79 int firmware_is_dpfw)
Stefan Reinauera2b67d32015-09-01 16:30:43 -070080{
Stefan Reinauerf70ff252019-11-13 18:58:16 -080081 unsigned char *data;
Stefan Reinauera2b67d32015-09-01 16:30:43 -070082 int i;
Stefan Reinauerf70ff252019-11-13 18:58:16 -080083 uint32_t id, rom_size = 0;
Stefan Reinauera2b67d32015-09-01 16:30:43 -070084 FILE *fw;
85
Stefan Reinauerf70ff252019-11-13 18:58:16 -080086 id = get_spi_flash_id(em100);
87 switch (id) {
88 case 0x202015:
89 rom_size = 2 MB;
90 break;
91 default:
92 printf("Unknown SPI flash id = %06x. Please report\n", id);
93 return 1;
94 }
95 data = malloc(rom_size);
96 if (data == NULL) {
97 perror("Out of memory.\n");
98 exit(1);
99 }
100 memset(data, 0, rom_size);
101
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700102 printf("\nWriting EM100Pro firmware to file %s\n", filename);
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800103 for (i=0; i < rom_size; i+=256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700104 if((i & 0x7fff) == 0)
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800105 print_progress(i * 100 / rom_size);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700106 if (!read_spi_flash_page(em100, i, data+i)) {
107 if (!read_spi_flash_page(em100, i, data+i))
108 if (!read_spi_flash_page(em100, i, data+i))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600109 printf("\nERROR: Couldn't read @%08x\n",
110 i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700111 }
112 }
113 print_progress(100);
114 fw = fopen(filename, "wb");
Stefan Reinauera9278a92015-09-01 16:16:27 -0700115 if (!fw) {
116 perror(filename);
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800117 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700118 return 0;
119 }
120
121 if (firmware_is_dpfw) {
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800122 int fpga_size = 0, mcu_size = 0, hdrversion = 0;
Stefan Reinauer9d603982017-11-20 12:40:05 -0800123 char all_ff[256];
Stefan Reinauera9278a92015-09-01 16:16:27 -0700124 char mcu_version[8];
125 char fpga_version[8];
126 unsigned char header[0x100];
127
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800128 switch (em100->hwversion) {
129 case HWVERSION_EM100PRO:
130 hdrversion=1;
131 break;
Stefan Reinauerf580b192019-11-18 15:24:22 -0800132 case HWVERSION_EM100PRO_G2:
133 hdrversion=2;
134 break;
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800135 default:
136 printf("Dumping DPFW firmware on hardware version %u is "
137 "not yet supported.\n", em100->hwversion);
138 exit(1);
139 }
140
Stefan Reinauer55730a82018-01-22 19:26:53 -0800141 memset(all_ff, 255, sizeof(all_ff));
Stefan Reinauera9278a92015-09-01 16:16:27 -0700142 for (i = 0; i < 0x100000; i+=0x100) {
143 if (memcmp(data+i, all_ff, 256) == 0)
144 break;
145 }
146 if (i == 0x100000) {
147 printf("Can't parse device firmware. Please extract"
148 " raw firmware instead.\n");
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800149 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700150 exit(1);
151 }
152 fpga_size = i;
153
154 for (i = 0; i < 0xfff00; i+=0x100) {
155 if (memcmp(data+0x100100+i, all_ff, 256) == 0)
156 break;
157 }
158 if (i == 0xfff00) {
159 printf("Can't parse device firmware. Please extract"
160 " raw firmware instead.\n");
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800161 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700162 exit(1);
163 }
164 mcu_size = i;
165
166 snprintf(mcu_version, 8, "%d.%d",
167 em100->mcu >> 8, em100->mcu & 0xff);
168 snprintf(fpga_version, 8, "%d.%d",
169 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff);
170
171 memset(header, 0, 0x100);
Stefan Reinauerf580b192019-11-18 15:24:22 -0800172 switch (hdrversion) {
173 case 1:
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800174 memcpy(header, "em100pro", 8);
Stefan Reinauerf580b192019-11-18 15:24:22 -0800175 break;
176 case 2:
177 memcpy(header, "EM100Pro-G2", 11);
178 break;
179 }
Stefan Reinauera9278a92015-09-01 16:16:27 -0700180 memcpy(header + 0x28, "WFPD", 4);
181 memcpy(header + 0x14, mcu_version, 4);
182 memcpy(header + 0x1e, fpga_version, 4);
183 put_le32(header + 0x38, 0x100);
184 put_le32(header + 0x3c, fpga_size);
185 put_le32(header + 0x40, 0x100 + fpga_size);
186 put_le32(header + 0x44, 0x100 + mcu_size);
187 fwrite(header, 0x100, 1, fw);
188 fwrite(data, fpga_size, 1, fw);
189 fwrite(data + 0x100100, mcu_size, 1, fw);
190 } else {
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800191 if (fwrite(data, rom_size, 1, fw) != 1)
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700192 printf("ERROR: Couldn't write %s\n", filename);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700193 }
Stefan Reinauera9278a92015-09-01 16:16:27 -0700194 fclose(fw);
195
Stefan Reinauer6a5ed5b2019-11-19 14:52:16 -0800196#ifdef DEBUG
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800197 hexdump(data, rom_size);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700198#endif
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800199 free(data);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700200 return 0;
201}
202
203int firmware_update(struct em100 *em100, const char *filename, int verify)
204{
Stefan Reinauerb825c922015-10-10 09:05:58 +0000205#define MAX_VERSION_LENGTH 10
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700206 unsigned char page[256], vpage[256];
207 FILE *f;
208 long fsize;
209 unsigned char *fw;
210 int i;
Stefan Reinauer826d6682015-10-10 08:33:41 +0000211 int fpga_offset, fpga_len, mcu_offset, mcu_len;
Stefan Reinauerb825c922015-10-10 09:05:58 +0000212 char fpga_version[MAX_VERSION_LENGTH + 1],
213 mcu_version[MAX_VERSION_LENGTH + 1];
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700214
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800215 switch (em100->hwversion) {
216 case HWVERSION_EM100PRO:
Stefan Reinauerf580b192019-11-18 15:24:22 -0800217 printf("Detected EM100Pro (original).\n");
218 break;
219 case HWVERSION_EM100PRO_G2:
220 printf("Detected EM100Pro-G2.\n");
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800221 break;
222 default:
223 printf("Updating EM100Pro firmware on hardware version %u is "
224 "not yet supported.\n", em100->hwversion);
225 exit(1);
226 }
227
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700228 printf("\nAttempting firmware update with file %s\n", filename);
229
230 f = fopen(filename, "rb");
231 if (!f) {
232 perror(filename);
233 return 0;
234 }
235
236 fseek(f, 0, SEEK_END);
237 fsize = ftell(f);
Stefan Reinauer85c22ea2015-10-09 13:53:56 +0000238 if (fsize < 0) {
239 perror(filename);
Stefan Reinauer95b67a02015-10-10 10:02:02 +0000240 fclose(f);
Stefan Reinauer85c22ea2015-10-09 13:53:56 +0000241 return 0;
242 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700243 fseek(f, 0, SEEK_SET);
244
245 fw = malloc(fsize);
Patrick Georgi21bbdb92019-08-23 13:15:34 +0200246 if (!fw) {
247 printf("ERROR: out of memory.\n");
248 fclose(f);
249 return 0;
250 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700251 if (fread(fw, fsize, 1, f) != 1) {
252 perror(filename);
253 fclose(f);
Stefan Reinauerb02440f2015-10-10 08:45:30 +0000254 free(fw);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700255 return 0;
256 }
257 fclose(f);
258
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800259 if (em100->hwversion == HWVERSION_EM100PRO && (memcmp(fw, "em100pro", 8) != 0 ||
260 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
Stefan Reinauerf580b192019-11-18 15:24:22 -0800261 printf("ERROR: Not an EM100Pro (original) firmware file.\n");
262 free(fw);
263 return 0;
264 }
265
266 if (em100->hwversion == HWVERSION_EM100PRO_G2 && (memcmp(fw, "EM100Pro-G2", 11) != 0 ||
267 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
268 printf("ERROR: Not an EM100Pro-G2 firmware file.\n");
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700269 free(fw);
270 return 0;
271 }
272
Stefan Reinauer826d6682015-10-10 08:33:41 +0000273 /* Find firmwares in the update file */
274 fpga_offset = get_le32(fw+0x38);
275 fpga_len = get_le32(fw+0x3c);
276 mcu_offset = get_le32(fw+0x40);
277 mcu_len = get_le32(fw+0x44);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700278
Stefan Reinauerb825c922015-10-10 09:05:58 +0000279 /* Extracting versions */
280 strncpy(mcu_version, (char *)fw + 0x14, MAX_VERSION_LENGTH);
281 mcu_version[MAX_VERSION_LENGTH] = '\0';
282 strncpy(fpga_version, (char *)fw + 0x1e, MAX_VERSION_LENGTH);
283 fpga_version[MAX_VERSION_LENGTH] = '\0';
284
Stefan Reinauerf580b192019-11-18 15:24:22 -0800285 printf("EM100Pro%s Update File: %s\n",
286 em100->hwversion == HWVERSION_EM100PRO_G2 ? "-G2" : "", filename);
Stefan Reinauerd3c3df22019-11-20 23:59:36 -0800287 if (em100->hwversion == HWVERSION_EM100PRO)
288 printf(" Installed version: MCU %d.%d, FPGA %d.%d (%s)\n",
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700289 em100->mcu >> 8, em100->mcu & 0xff,
290 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff,
291 em100->fpga & 0x8000 ? "1.8V" : "3.3V");
Stefan Reinauerd3c3df22019-11-20 23:59:36 -0800292 else
293 printf(" Installed version: MCU %d.%d, FPGA %d.%03d\n",
294 em100->mcu >> 8, em100->mcu & 0xff,
295 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff);
296
Stefan Reinauerb825c922015-10-10 09:05:58 +0000297 printf(" New version: MCU %s, FPGA %s\n",
298 mcu_version, fpga_version);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700299
Stefan Reinauerfba6eb32015-10-10 10:22:12 +0000300 if (fpga_len < 256 || mcu_len < 256 ||
301 fpga_len > 0x100000 || mcu_len > 0xf0000) {
Stefan Reinauer2d836162015-10-10 09:40:07 +0000302 printf("\nFirmware file not valid.\n");
303 free(fw);
304 return 0;
305 }
306
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700307 /* Unlock and erase sector. Reading
308 * the SPI flash ID is requires to
309 * actually unlock the chip.
310 */
311 unlock_spi_flash(em100);
312 get_spi_flash_id(em100);
313
314 printf("Erasing firmware:\n");
315 for (i=0; i<=0x1e; i++) {
316 print_progress(i * 100 / 0x1e);
317 erase_spi_flash_sector(em100, i);
318 }
319 get_spi_flash_id(em100); // Needed?
320
321 printf("Writing firmware:\n");
322
323 /* Writing FPGA firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000324 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700325 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000326 memcpy(page, fw + fpga_offset + i, fpga_len - i
327 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700328 write_spi_flash_page(em100, i, page);
329 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000330 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700331 }
332
333 /* Writing MCU firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000334 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700335 memset(page, 0xff, 256);
Stefan Reinauer1205b2c2015-10-10 13:38:16 +0000336 memcpy(page, fw + mcu_offset + i, (mcu_len - i)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000337 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700338 write_spi_flash_page(em100, i + 0x100100, page);
339 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000340 print_progress(((fpga_len + i) * 100) /
341 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700342 }
343 print_progress(100);
344
345 if (verify) {
346 printf("Verifying firmware:\n");
Stefan Reinauer826d6682015-10-10 08:33:41 +0000347 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700348 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000349 memcpy(page, fw + fpga_offset + i, fpga_len - i
350 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700351 read_spi_flash_page(em100, i, vpage);
352
353 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000354 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700355 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600356 printf("\nERROR: Could not write FPGA firmware"
357 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700358 }
Stefan Reinauer826d6682015-10-10 08:33:41 +0000359 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700360 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000361 memcpy(page, fw + mcu_offset + i, mcu_len - i
362 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700363 read_spi_flash_page(em100, i + 0x100100, vpage);
364
365 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000366 print_progress(((fpga_len + i) * 100) /
367 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700368 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600369 printf("\nERROR: Could not write MCU firmware"
370 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700371 }
372 }
373 print_progress(100);
374
375 free(fw);
376
Stefan Reinauerf580b192019-11-18 15:24:22 -0800377 /* Write magic update tag '.UBOOTU.' */
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700378 memset(page, 0x00, 256);
379 page[0] = 0xaa;
380 page[1] = 0x55;
381 page[2] = 0x42;
382 page[3] = 0x4f;
383 page[4] = 0x4f;
384 page[5] = 0x54;
385 page[6] = 0x55;
386 page[7] = 0xaa;
387 write_spi_flash_page(em100, 0x100000, page);
388
389 if (verify) {
390 read_spi_flash_page(em100, 0x100000, vpage);
391 if (memcmp(page, vpage, 256))
392 printf("ERROR: Could not write update tag.\n");
393 }
394
395 printf("\nDisconnect and reconnect your EM100pro\n");
396
397 return 1;
398}