blob: dd1a173a2dd2fdb78a62c0a3ca669e9b86684de8 [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;
132 default:
133 printf("Dumping DPFW firmware on hardware version %u is "
134 "not yet supported.\n", em100->hwversion);
135 exit(1);
136 }
137
Stefan Reinauer55730a82018-01-22 19:26:53 -0800138 memset(all_ff, 255, sizeof(all_ff));
Stefan Reinauera9278a92015-09-01 16:16:27 -0700139 for (i = 0; i < 0x100000; i+=0x100) {
140 if (memcmp(data+i, all_ff, 256) == 0)
141 break;
142 }
143 if (i == 0x100000) {
144 printf("Can't parse device firmware. Please extract"
145 " raw firmware instead.\n");
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800146 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700147 exit(1);
148 }
149 fpga_size = i;
150
151 for (i = 0; i < 0xfff00; i+=0x100) {
152 if (memcmp(data+0x100100+i, all_ff, 256) == 0)
153 break;
154 }
155 if (i == 0xfff00) {
156 printf("Can't parse device firmware. Please extract"
157 " raw firmware instead.\n");
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800158 free(data);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700159 exit(1);
160 }
161 mcu_size = i;
162
163 snprintf(mcu_version, 8, "%d.%d",
164 em100->mcu >> 8, em100->mcu & 0xff);
165 snprintf(fpga_version, 8, "%d.%d",
166 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff);
167
168 memset(header, 0, 0x100);
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800169 if (hdrversion == 1)
170 memcpy(header, "em100pro", 8);
Stefan Reinauera9278a92015-09-01 16:16:27 -0700171 memcpy(header + 0x28, "WFPD", 4);
172 memcpy(header + 0x14, mcu_version, 4);
173 memcpy(header + 0x1e, fpga_version, 4);
174 put_le32(header + 0x38, 0x100);
175 put_le32(header + 0x3c, fpga_size);
176 put_le32(header + 0x40, 0x100 + fpga_size);
177 put_le32(header + 0x44, 0x100 + mcu_size);
178 fwrite(header, 0x100, 1, fw);
179 fwrite(data, fpga_size, 1, fw);
180 fwrite(data + 0x100100, mcu_size, 1, fw);
181 } else {
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800182 if (fwrite(data, rom_size, 1, fw) != 1)
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700183 printf("ERROR: Couldn't write %s\n", filename);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700184 }
Stefan Reinauera9278a92015-09-01 16:16:27 -0700185 fclose(fw);
186
Stefan Reinauer6a5ed5b2019-11-19 14:52:16 -0800187#ifdef DEBUG
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800188 hexdump(data, rom_size);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700189#endif
Stefan Reinauerf70ff252019-11-13 18:58:16 -0800190 free(data);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700191 return 0;
192}
193
194int firmware_update(struct em100 *em100, const char *filename, int verify)
195{
Stefan Reinauerb825c922015-10-10 09:05:58 +0000196#define MAX_VERSION_LENGTH 10
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700197 unsigned char page[256], vpage[256];
198 FILE *f;
199 long fsize;
200 unsigned char *fw;
201 int i;
Stefan Reinauer826d6682015-10-10 08:33:41 +0000202 int fpga_offset, fpga_len, mcu_offset, mcu_len;
Stefan Reinauerb825c922015-10-10 09:05:58 +0000203 char fpga_version[MAX_VERSION_LENGTH + 1],
204 mcu_version[MAX_VERSION_LENGTH + 1];
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700205
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800206 switch (em100->hwversion) {
207 case HWVERSION_EM100PRO:
208 printf("Detected EM100Pro-G1.\n");
209 break;
210 default:
211 printf("Updating EM100Pro firmware on hardware version %u is "
212 "not yet supported.\n", em100->hwversion);
213 exit(1);
214 }
215
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700216 printf("\nAttempting firmware update with file %s\n", filename);
217
218 f = fopen(filename, "rb");
219 if (!f) {
220 perror(filename);
221 return 0;
222 }
223
224 fseek(f, 0, SEEK_END);
225 fsize = ftell(f);
Stefan Reinauer85c22ea2015-10-09 13:53:56 +0000226 if (fsize < 0) {
227 perror(filename);
Stefan Reinauer95b67a02015-10-10 10:02:02 +0000228 fclose(f);
Stefan Reinauer85c22ea2015-10-09 13:53:56 +0000229 return 0;
230 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700231 fseek(f, 0, SEEK_SET);
232
233 fw = malloc(fsize);
Patrick Georgi21bbdb92019-08-23 13:15:34 +0200234 if (!fw) {
235 printf("ERROR: out of memory.\n");
236 fclose(f);
237 return 0;
238 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700239 if (fread(fw, fsize, 1, f) != 1) {
240 perror(filename);
241 fclose(f);
Stefan Reinauerb02440f2015-10-10 08:45:30 +0000242 free(fw);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700243 return 0;
244 }
245 fclose(f);
246
Stefan Reinauerfc22de32019-11-13 19:14:12 -0800247 if (em100->hwversion == HWVERSION_EM100PRO && (memcmp(fw, "em100pro", 8) != 0 ||
248 memcmp(fw + 0x28, "WFPD", 4) != 0)) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700249 printf("ERROR: Not an EM100Pro firmware file.\n");
250 free(fw);
251 return 0;
252 }
253
Stefan Reinauer826d6682015-10-10 08:33:41 +0000254 /* Find firmwares in the update file */
255 fpga_offset = get_le32(fw+0x38);
256 fpga_len = get_le32(fw+0x3c);
257 mcu_offset = get_le32(fw+0x40);
258 mcu_len = get_le32(fw+0x44);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700259
Stefan Reinauerb825c922015-10-10 09:05:58 +0000260 /* Extracting versions */
261 strncpy(mcu_version, (char *)fw + 0x14, MAX_VERSION_LENGTH);
262 mcu_version[MAX_VERSION_LENGTH] = '\0';
263 strncpy(fpga_version, (char *)fw + 0x1e, MAX_VERSION_LENGTH);
264 fpga_version[MAX_VERSION_LENGTH] = '\0';
265
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700266 printf("EM100Pro Update File: %s\n", filename);
267 printf(" Installed version: MCU %d.%d, FPGA %d.%d (%s)\n",
268 em100->mcu >> 8, em100->mcu & 0xff,
269 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff,
270 em100->fpga & 0x8000 ? "1.8V" : "3.3V");
Stefan Reinauerb825c922015-10-10 09:05:58 +0000271 printf(" New version: MCU %s, FPGA %s\n",
272 mcu_version, fpga_version);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700273
Stefan Reinauerfba6eb32015-10-10 10:22:12 +0000274 if (fpga_len < 256 || mcu_len < 256 ||
275 fpga_len > 0x100000 || mcu_len > 0xf0000) {
Stefan Reinauer2d836162015-10-10 09:40:07 +0000276 printf("\nFirmware file not valid.\n");
277 free(fw);
278 return 0;
279 }
280
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700281 /* Unlock and erase sector. Reading
282 * the SPI flash ID is requires to
283 * actually unlock the chip.
284 */
285 unlock_spi_flash(em100);
286 get_spi_flash_id(em100);
287
288 printf("Erasing firmware:\n");
289 for (i=0; i<=0x1e; i++) {
290 print_progress(i * 100 / 0x1e);
291 erase_spi_flash_sector(em100, i);
292 }
293 get_spi_flash_id(em100); // Needed?
294
295 printf("Writing firmware:\n");
296
297 /* Writing FPGA firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000298 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700299 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000300 memcpy(page, fw + fpga_offset + i, fpga_len - i
301 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700302 write_spi_flash_page(em100, i, page);
303 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000304 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700305 }
306
307 /* Writing MCU firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000308 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700309 memset(page, 0xff, 256);
Stefan Reinauer1205b2c2015-10-10 13:38:16 +0000310 memcpy(page, fw + mcu_offset + i, (mcu_len - i)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000311 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700312 write_spi_flash_page(em100, i + 0x100100, page);
313 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000314 print_progress(((fpga_len + i) * 100) /
315 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700316 }
317 print_progress(100);
318
319 if (verify) {
320 printf("Verifying firmware:\n");
Stefan Reinauer826d6682015-10-10 08:33:41 +0000321 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700322 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000323 memcpy(page, fw + fpga_offset + i, fpga_len - i
324 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700325 read_spi_flash_page(em100, i, vpage);
326
327 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000328 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700329 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600330 printf("\nERROR: Could not write FPGA firmware"
331 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700332 }
Stefan Reinauer826d6682015-10-10 08:33:41 +0000333 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700334 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000335 memcpy(page, fw + mcu_offset + i, mcu_len - i
336 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700337 read_spi_flash_page(em100, i + 0x100100, vpage);
338
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 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600343 printf("\nERROR: Could not write MCU firmware"
344 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700345 }
346 }
347 print_progress(100);
348
349 free(fw);
350
351 /* Write magic update page */
352 memset(page, 0x00, 256);
353 page[0] = 0xaa;
354 page[1] = 0x55;
355 page[2] = 0x42;
356 page[3] = 0x4f;
357 page[4] = 0x4f;
358 page[5] = 0x54;
359 page[6] = 0x55;
360 page[7] = 0xaa;
361 write_spi_flash_page(em100, 0x100000, page);
362
363 if (verify) {
364 read_spi_flash_page(em100, 0x100000, vpage);
365 if (memcmp(page, vpage, 256))
366 printf("ERROR: Could not write update tag.\n");
367 }
368
369 printf("\nDisconnect and reconnect your EM100pro\n");
370
371 return 1;
372}