blob: baeb996b50b751c11193dec410bf3cda7d8c5611 [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{
81#define ROM_SIZE (2*1024*1024)
82 unsigned char data[ROM_SIZE];
83 int i;
84 FILE *fw;
85
86 printf("\nWriting EM100Pro firmware to file %s\n", filename);
87 memset(data, 0, ROM_SIZE);
88 for (i=0; i < ROM_SIZE; i+=256) {
89 if((i & 0x7fff) == 0)
90 print_progress(i * 100 / ROM_SIZE);
91 if (!read_spi_flash_page(em100, i, data+i)) {
92 if (!read_spi_flash_page(em100, i, data+i))
93 if (!read_spi_flash_page(em100, i, data+i))
Martin Rothb54d6ba2015-09-29 14:49:37 -060094 printf("\nERROR: Couldn't read @%08x\n",
95 i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -070096 }
97 }
98 print_progress(100);
99 fw = fopen(filename, "wb");
Stefan Reinauera9278a92015-09-01 16:16:27 -0700100 if (!fw) {
101 perror(filename);
102 return 0;
103 }
104
105 if (firmware_is_dpfw) {
106 int fpga_size = 0, mcu_size = 0;
107 char *all_ff[256];
108 char mcu_version[8];
109 char fpga_version[8];
110 unsigned char header[0x100];
111
112 memset(all_ff, 255, 256);
113 for (i = 0; i < 0x100000; i+=0x100) {
114 if (memcmp(data+i, all_ff, 256) == 0)
115 break;
116 }
117 if (i == 0x100000) {
118 printf("Can't parse device firmware. Please extract"
119 " raw firmware instead.\n");
120 exit(1);
121 }
122 fpga_size = i;
123
124 for (i = 0; i < 0xfff00; i+=0x100) {
125 if (memcmp(data+0x100100+i, all_ff, 256) == 0)
126 break;
127 }
128 if (i == 0xfff00) {
129 printf("Can't parse device firmware. Please extract"
130 " raw firmware instead.\n");
131 exit(1);
132 }
133 mcu_size = i;
134
135 snprintf(mcu_version, 8, "%d.%d",
136 em100->mcu >> 8, em100->mcu & 0xff);
137 snprintf(fpga_version, 8, "%d.%d",
138 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff);
139
140 memset(header, 0, 0x100);
141 memcpy(header, "em100pro", 8);
142 memcpy(header + 0x28, "WFPD", 4);
143 memcpy(header + 0x14, mcu_version, 4);
144 memcpy(header + 0x1e, fpga_version, 4);
145 put_le32(header + 0x38, 0x100);
146 put_le32(header + 0x3c, fpga_size);
147 put_le32(header + 0x40, 0x100 + fpga_size);
148 put_le32(header + 0x44, 0x100 + mcu_size);
149 fwrite(header, 0x100, 1, fw);
150 fwrite(data, fpga_size, 1, fw);
151 fwrite(data + 0x100100, mcu_size, 1, fw);
152 } else {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700153 if (fwrite(data, ROM_SIZE, 1, fw) != 1)
154 printf("ERROR: Couldn't write %s\n", filename);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700155 }
Stefan Reinauera9278a92015-09-01 16:16:27 -0700156 fclose(fw);
157
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700158#if DEBUG
159 hexdump(data, ROM_SIZE);
160#endif
161 return 0;
162}
163
164int firmware_update(struct em100 *em100, const char *filename, int verify)
165{
Stefan Reinauerb825c922015-10-10 09:05:58 +0000166#define MAX_VERSION_LENGTH 10
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700167 unsigned char page[256], vpage[256];
168 FILE *f;
169 long fsize;
170 unsigned char *fw;
171 int i;
Stefan Reinauer826d6682015-10-10 08:33:41 +0000172 int fpga_offset, fpga_len, mcu_offset, mcu_len;
Stefan Reinauerb825c922015-10-10 09:05:58 +0000173 char fpga_version[MAX_VERSION_LENGTH + 1],
174 mcu_version[MAX_VERSION_LENGTH + 1];
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700175
176 printf("\nAttempting firmware update with file %s\n", filename);
177
178 f = fopen(filename, "rb");
179 if (!f) {
180 perror(filename);
181 return 0;
182 }
183
184 fseek(f, 0, SEEK_END);
185 fsize = ftell(f);
Stefan Reinauer85c22ea2015-10-09 13:53:56 +0000186 if (fsize < 0) {
187 perror(filename);
188 return 0;
189 }
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700190 fseek(f, 0, SEEK_SET);
191
192 fw = malloc(fsize);
193 if (fread(fw, fsize, 1, f) != 1) {
194 perror(filename);
195 fclose(f);
Stefan Reinauerb02440f2015-10-10 08:45:30 +0000196 free(fw);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700197 return 0;
198 }
199 fclose(f);
200
201 if (memcmp(fw, "em100pro", 8) != 0 ||
202 memcmp(fw + 0x28, "WFPD", 4) != 0) {
203 printf("ERROR: Not an EM100Pro firmware file.\n");
204 free(fw);
205 return 0;
206 }
207
Stefan Reinauer826d6682015-10-10 08:33:41 +0000208 /* Find firmwares in the update file */
209 fpga_offset = get_le32(fw+0x38);
210 fpga_len = get_le32(fw+0x3c);
211 mcu_offset = get_le32(fw+0x40);
212 mcu_len = get_le32(fw+0x44);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700213
Stefan Reinauerb825c922015-10-10 09:05:58 +0000214 /* Extracting versions */
215 strncpy(mcu_version, (char *)fw + 0x14, MAX_VERSION_LENGTH);
216 mcu_version[MAX_VERSION_LENGTH] = '\0';
217 strncpy(fpga_version, (char *)fw + 0x1e, MAX_VERSION_LENGTH);
218 fpga_version[MAX_VERSION_LENGTH] = '\0';
219
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700220 printf("EM100Pro Update File: %s\n", filename);
221 printf(" Installed version: MCU %d.%d, FPGA %d.%d (%s)\n",
222 em100->mcu >> 8, em100->mcu & 0xff,
223 em100->fpga >> 8 & 0x7f, em100->fpga & 0xff,
224 em100->fpga & 0x8000 ? "1.8V" : "3.3V");
Stefan Reinauerb825c922015-10-10 09:05:58 +0000225 printf(" New version: MCU %s, FPGA %s\n",
226 mcu_version, fpga_version);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700227
228 /* Unlock and erase sector. Reading
229 * the SPI flash ID is requires to
230 * actually unlock the chip.
231 */
232 unlock_spi_flash(em100);
233 get_spi_flash_id(em100);
234
235 printf("Erasing firmware:\n");
236 for (i=0; i<=0x1e; i++) {
237 print_progress(i * 100 / 0x1e);
238 erase_spi_flash_sector(em100, i);
239 }
240 get_spi_flash_id(em100); // Needed?
241
242 printf("Writing firmware:\n");
243
244 /* Writing FPGA firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000245 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700246 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000247 memcpy(page, fw + fpga_offset + i, fpga_len - i
248 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700249 write_spi_flash_page(em100, i, page);
250 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000251 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700252 }
253
254 /* Writing MCU firmware */
Stefan Reinauer826d6682015-10-10 08:33:41 +0000255 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700256 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000257 memcpy(page, fw + mcu_offset + i, mcu_len - i
258 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700259 write_spi_flash_page(em100, i + 0x100100, page);
260 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000261 print_progress(((fpga_len + i) * 100) /
262 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700263 }
264 print_progress(100);
265
266 if (verify) {
267 printf("Verifying firmware:\n");
Stefan Reinauer826d6682015-10-10 08:33:41 +0000268 for (i = 0; i < fpga_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700269 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000270 memcpy(page, fw + fpga_offset + i, fpga_len - i
271 > 256 ? 256 : fpga_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700272 read_spi_flash_page(em100, i, vpage);
273
274 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000275 print_progress((i * 100) / (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700276 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600277 printf("\nERROR: Could not write FPGA firmware"
278 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700279 }
Stefan Reinauer826d6682015-10-10 08:33:41 +0000280 for (i = 0; i < mcu_len; i += 256) {
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700281 memset(page, 0xff, 256);
Stefan Reinauer826d6682015-10-10 08:33:41 +0000282 memcpy(page, fw + mcu_offset + i, mcu_len - i
283 > 256 ? 256 : mcu_len - i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700284 read_spi_flash_page(em100, i + 0x100100, vpage);
285
286 if ((i & 0xfff) == 0)
Stefan Reinauer826d6682015-10-10 08:33:41 +0000287 print_progress(((fpga_len + i) * 100) /
288 (fpga_len + mcu_len));
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700289 if (memcmp(page, vpage, 256))
Martin Rothb54d6ba2015-09-29 14:49:37 -0600290 printf("\nERROR: Could not write MCU firmware"
291 " (%x).\n", i);
Stefan Reinauera2b67d32015-09-01 16:30:43 -0700292 }
293 }
294 print_progress(100);
295
296 free(fw);
297
298 /* Write magic update page */
299 memset(page, 0x00, 256);
300 page[0] = 0xaa;
301 page[1] = 0x55;
302 page[2] = 0x42;
303 page[3] = 0x4f;
304 page[4] = 0x4f;
305 page[5] = 0x54;
306 page[6] = 0x55;
307 page[7] = 0xaa;
308 write_spi_flash_page(em100, 0x100000, page);
309
310 if (verify) {
311 read_spi_flash_page(em100, 0x100000, vpage);
312 if (memcmp(page, vpage, 256))
313 printf("ERROR: Could not write update tag.\n");
314 }
315
316 printf("\nDisconnect and reconnect your EM100pro\n");
317
318 return 1;
319}