blob: c88011c15381f82c827e987cf7e70ab962ae446f [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) {
122 int fpga_size = 0, mcu_size = 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 Reinauer55730a82018-01-22 19:26:53 -0800128 memset(all_ff, 255, sizeof(all_ff));
Stefan Reinauera9278a92015-09-01 16:16:27 -0700129 for (i = 0; i < 0x100000; i+=0x100) {
130 if (memcmp(data+i, all_ff, 256) == 0)
131 break;
132 }
133 if (i == 0x100000) {
134 printf("Can't parse device firmware. Please extract"
135 " raw firmware instead.\n");
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800136 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700137 exit(1);
138 }
139 fpga_size = i;
140
141 for (i = 0; i < 0xfff00; i+=0x100) {
142 if (memcmp(data+0x100100+i, all_ff, 256) == 0)
143 break;
144 }
145 if (i == 0xfff00) {
146 printf("Can't parse device firmware. Please extract"
147 " raw firmware instead.\n");
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800148 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700149 exit(1);
150 }
151 mcu_size = i;
152
153 snprintf(mcu_version, 8, "%d.%d",
154 em100->mcu >> 8, em100->mcu & 0xff);
155 snprintf(fpga_version, 8, "%d.%d",
156 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff);
157
158 memset(header, 0, 0x100);
159 memcpy(header, "em100pro", 8);
160 memcpy(header + 0x28, "WFPD", 4);
161 memcpy(header + 0x14, mcu_version, 4);
162 memcpy(header + 0x1e, fpga_version, 4);
163 put_le32(header + 0x38, 0x100);
164 put_le32(header + 0x3c, fpga_size);
165 put_le32(header + 0x40, 0x100 + fpga_size);
166 put_le32(header + 0x44, 0x100 + mcu_size);
167 fwrite(header, 0x100, 1, fw);
168 fwrite(data, fpga_size, 1, fw);
169 fwrite(data + 0x100100, mcu_size, 1, fw);
170 } else {
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800171 if (fwrite(data, rom_size, 1, fw) != 1)
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700172 printf("ERROR: Couldn't write %s\n", filename);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700173 }
Stefan Reinauera9278a92015-09-01 16:16:27 -0700174 fclose(fw);
175
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700176#if DEBUG
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800177 hexdump(data, rom_size);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700178#endif
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800179 free(data);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700180 return 0;
181}
182
183int firmware_update(struct em100 *em100, const char *filename, int verify)
184{
Stefan Reinauerb825c922015-10-10 09:05:58 +0000185#define MAX_VERSION_LENGTH 10
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700186 unsigned char page[256], vpage[256];
187 FILE *f;
188 long fsize;
189 unsigned char *fw;
190 int i;
Stefan Reinauer826d6682015-10-10 08:33:41 +0000191 int fpga_offset, fpga_len, mcu_offset, mcu_len;
Stefan Reinauerb825c922015-10-10 09:05:58 +0000192 char fpga_version[MAX_VERSION_LENGTH + 1],
193 mcu_version[MAX_VERSION_LENGTH + 1];
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700194
195 printf("\nAttempting firmware update with file %s\n", filename);
196
197 f = fopen(filename, "rb");
198 if (!f) {
199 perror(filename);
200 return 0;
201 }
202
203 fseek(f, 0, SEEK_END);
204 fsize = ftell(f);
Stefan Reinauer85c22ea2015-10-09 13:53:56 +0000205 if (fsize < 0) {
206 perror(filename);
Stefan Reinauer95b67a02015-10-10 10:02:02 +0000207 fclose(f);
Stefan Reinauer85c22ea2015-10-09 13:53:56 +0000208 return 0;
209 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700210 fseek(f, 0, SEEK_SET);
211
212 fw = malloc(fsize);
Patrick Georgi21bbdb92019-08-23 13:15:34 +0200213 if (!fw) {
214 printf("ERROR: out of memory.\n");
215 fclose(f);
216 return 0;
217 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700218 if (fread(fw, fsize, 1, f) != 1) {
219 perror(filename);
220 fclose(f);
Stefan Reinauerb02440f2015-10-10 08:45:30 +0000221 free(fw);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700222 return 0;
223 }
224 fclose(f);
225
226 if (memcmp(fw, "em100pro", 8) != 0 ||
227 memcmp(fw + 0x28, "WFPD", 4) != 0) {
228 printf("ERROR: Not an EM100Pro firmware file.\n");
229 free(fw);
230 return 0;
231 }
232
Stefan Reinauer826d6682015-10-10 08:33:41 +0000233 /* Find firmwares in the update file */
234 fpga_offset = get_le32(fw+0x38);
235 fpga_len = get_le32(fw+0x3c);
236 mcu_offset = get_le32(fw+0x40);
237 mcu_len = get_le32(fw+0x44);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700238
Stefan Reinauerb825c922015-10-10 09:05:58 +0000239 /* Extracting versions */
240 strncpy(mcu_version, (char *)fw + 0x14, MAX_VERSION_LENGTH);
241 mcu_version[MAX_VERSION_LENGTH] = '\0';
242 strncpy(fpga_version, (char *)fw + 0x1e, MAX_VERSION_LENGTH);
243 fpga_version[MAX_VERSION_LENGTH] = '\0';
244
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700245 printf("EM100Pro Update File: %s\n", filename);
246 printf(" Installed version: MCU %d.%d, FPGA %d.%d (%s)\n",
247 em100->mcu >> 8, em100->mcu & 0xff,
248 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff,
249 em100->fpga & 0x8000 ? "1.8V" : "3.3V");
Stefan Reinauerb825c922015-10-10 09:05:58 +0000250 printf(" New version: MCU %s, FPGA %s\n",
251 mcu_version, fpga_version);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700252
Stefan Reinauerfba6eb32015-10-10 10:22:12 +0000253 if (fpga_len < 256 || mcu_len < 256 ||
254 fpga_len > 0x100000 || mcu_len > 0xf0000) {
Stefan Reinauer2d836162015-10-10 09:40:07 +0000255 printf("\nFirmware file not valid.\n");
256 free(fw);
257 return 0;
258 }
259
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700260 /* Unlock and erase sector. Reading
261 * the SPI flash ID is requires to
262 * actually unlock the chip.
263 */
264 unlock_spi_flash(em100);
265 get_spi_flash_id(em100);
266
267 printf("Erasing firmware:\n");
268 for (i=0; i<=0x1e; i++) {
269 print_progress(i * 100 / 0x1e);
270 erase_spi_flash_sector(em100, i);
271 }
272 get_spi_flash_id(em100); // Needed?
273
274 printf("Writing firmware:\n");
275
276 /* Writing FPGA firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000277 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700278 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000279 memcpy(page, fw + fpga_offset + i, fpga_len - i
280 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700281 write_spi_flash_page(em100, i, page);
282 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000283 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700284 }
285
286 /* Writing MCU firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000287 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700288 memset(page, 0xff, 256);
Stefan Reinauer1205b2c2015-10-10 13:38:16 +0000289 memcpy(page, fw + mcu_offset + i, (mcu_len - i)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000290 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700291 write_spi_flash_page(em100, i + 0x100100, page);
292 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000293 print_progress(((fpga_len + i) * 100) /
294 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700295 }
296 print_progress(100);
297
298 if (verify) {
299 printf("Verifying firmware:\n");
Stefan Reinauer826d6682015-10-10 08:33:41 +0000300 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700301 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000302 memcpy(page, fw + fpga_offset + i, fpga_len - i
303 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700304 read_spi_flash_page(em100, i, vpage);
305
306 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000307 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700308 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600309 printf("\nERROR: Could not write FPGA firmware"
310 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700311 }
Stefan Reinauer826d6682015-10-10 08:33:41 +0000312 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700313 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000314 memcpy(page, fw + mcu_offset + i, mcu_len - i
315 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700316 read_spi_flash_page(em100, i + 0x100100, vpage);
317
318 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000319 print_progress(((fpga_len + i) * 100) /
320 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700321 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600322 printf("\nERROR: Could not write MCU firmware"
323 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700324 }
325 }
326 print_progress(100);
327
328 free(fw);
329
330 /* Write magic update page */
331 memset(page, 0x00, 256);
332 page[0] = 0xaa;
333 page[1] = 0x55;
334 page[2] = 0x42;
335 page[3] = 0x4f;
336 page[4] = 0x4f;
337 page[5] = 0x54;
338 page[6] = 0x55;
339 page[7] = 0xaa;
340 write_spi_flash_page(em100, 0x100000, page);
341
342 if (verify) {
343 read_spi_flash_page(em100, 0x100000, vpage);
344 if (memcmp(page, vpage, 256))
345 printf("ERROR: Could not write update tag.\n");
346 }
347
348 printf("\nDisconnect and reconnect your EM100pro\n");
349
350 return 1;
351}