blob: 35d95a7194d75998a45e9e0a4d6e119956a28e0f [file] [log] [blame]
Brian Silverman2d9cf662013-12-18 14:22:49 -08001#include <libgen.h>
2#include <string.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6
7#include <string>
8
9#include "aos/common/logging/logging.h"
10#include "aos/common/logging/logging_impl.h"
11
12extern "C" {
13#include "stm32flash/parsers/parser.h"
14#include "stm32flash/parsers/hex.h"
15#include "stm32flash/serial.h"
16#include "stm32flash/stm32.h"
17#include "stm32flash/init.h"
18}
19
20int main(int argc, char **argv) {
21 ::aos::logging::Init();
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080022 ::aos::logging::AddImplementation(
23 new ::aos::logging::StreamLogImplementation(stdout));
Brian Silverman2d9cf662013-12-18 14:22:49 -080024
25 if (argc < 2) {
26 fputs("Need an argument saying which target to download.\n", stderr);
27 return 1;
28 }
29 ::std::string target = argv[1];
30
31 ::std::string device = "/dev/ttyUSB0";
32 serial_baud_t baud_rate = SERIAL_BAUD_57600;
33
34 ::std::string filename =
35 ::std::string(dirname(strdup(argv[0]))) +
36 "/../../../bbb_cape/src/cape/.obj/" + target + ".hex";
37
38 int file = open(filename.c_str(), O_RDONLY);
39 if (file == -1) {
Brian Silvermaned030062013-12-20 21:03:47 -080040 filename = target;
41 file = open(filename.c_str(), O_RDONLY);
42 if (file == -1) {
43 LOG(FATAL, "open(%s, O_RDONLY) failed with %d: %s\n",
44 filename.c_str(), errno, strerror(errno));
45 } else {
46 LOG(INFO, "using filename %s from the command line\n", filename.c_str());
47 }
Brian Silverman2d9cf662013-12-18 14:22:49 -080048 }
49
50 uint16_t start_address = 0;
51 {
52 uint8_t buffer[1 /* : */ + 2 /* record size */ + 4 /* address */ +
53 2 /* record type */];
54 ssize_t bytes = read(file, buffer, sizeof(buffer));
55 if (close(file) == -1) {
56 LOG(FATAL, "close(%d) failed with %d: %s\n",
57 file, errno, strerror(errno));
58 }
59 if (bytes != sizeof(buffer)) {
60 LOG(FATAL, "read %zd bytes from %s instead of %zu\n",
61 bytes, filename.c_str(), sizeof(buffer));
62 }
63 if (buffer[0] != ':' || buffer[7] != '0' || buffer[8] != '0') {
64 LOG(FATAL, "%s isn't a valid hex file that we know how to handle\n",
65 filename.c_str());
66 }
67 for (int i = 0; i < 4; ++i) {
68 uint8_t digit = buffer[3 + i];
69 int value;
70 if (digit >= '0' && digit <= '9') {
71 value = digit - '0';
72 } else if (digit >= 'a' && digit <= 'f') {
73 value = digit - 'a';
74 } else if (digit >= 'A' && digit <= 'F') {
75 value = digit - 'A';
76 } else {
77 LOG(FATAL, "unknown hex digit %c\n", digit);
78 }
79 start_address |= value << (12 - (4 * i));
80 }
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080081 LOG(INFO, "start address = 0x%x\n", start_address);
Brian Silverman2d9cf662013-12-18 14:22:49 -080082 }
83
84 parser_t *parser = &PARSER_HEX;
85 void *p_st = parser->init();
86 if (p_st == NULL) {
87 LOG(FATAL, "%s parser failed to initialize.\n", parser->name);
88 }
89 if (parser->open(p_st, filename.c_str(), 0) != PARSER_ERR_OK) {
90 LOG(FATAL, "opening file %s failed\n", filename.c_str());
91 }
92
93 serial_t *serial = serial_open(device.c_str());
94 if (serial == NULL) {
95 LOG(FATAL, "failed to open serial port %s because of %d: %s\n",
96 device.c_str(), errno, strerror(errno));
97 }
98 if (serial_setup(serial, baud_rate,
99 SERIAL_BITS_8,
100 SERIAL_PARITY_EVEN,
101 SERIAL_STOPBIT_1) != SERIAL_ERR_OK) {
102 LOG(FATAL, "setting up serial port %s failed because of %d: %s\n",
103 device.c_str(), errno, strerror(errno));
104 }
105 LOG(INFO, "serial configuration: %s\n", serial_get_setup_str(serial));
106
107 if (init_bl_entry(serial, NULL /* GPIO sequence */) == 0) {
108 LOG(FATAL, "init_bl_entry(%p, NULL) failed\n", serial);
109 }
110 stm32_t *stm = stm32_init(serial, true);
111 if (stm == NULL) {
112 LOG(FATAL, "stm32_init(%p, true) failed\n", serial);
113 }
114
115 unsigned int last_byte = parser->size(p_st);
116 unsigned int size = last_byte - start_address;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800117
118 // An array of the sizes of each sector.
119 static const uint32_t kSectorSizes[12] = {0x4000, 0x4000, 0x4000, 0x4000,
120 0x10000, 0x20000, 0x20000, 0x20000,
121 0x20000, 0x20000, 0x20000, 0x20000};
122 static const int kNumSectors = sizeof(kSectorSizes) / sizeof(kSectorSizes[0]);
123 // The sector number that we're going to start writing at.
124 int start_sector = 0;
125 for (uint32_t address = 0; start_sector <= kNumSectors;
126 address += kSectorSizes[start_sector++]) {
127 if (start_sector == kNumSectors) {
128 LOG(FATAL, "start address %x is too big\n", start_address);
129 }
130 if (address > start_address) {
131 LOG(FATAL, "start address %x is not on a sector boundary\n",
132 start_address);
133 }
134 if (address == start_address) break;
135 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800136
137 // The first sector that we're not going to erase.
138 int end_sector = 0;
139 for (uint32_t address = 0; end_sector <= kNumSectors;
140 address += kSectorSizes[end_sector++]) {
141 if (address > start_address + size) break;
142 if (end_sector == kNumSectors) {
143 LOG(FATAL, "%x bytes beyond start address of %x is too big\n",
144 size, start_address);
145 }
146 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800147
Brian Silvermanb5d83562013-12-18 15:51:41 -0800148 if (!stm32_erase_memory(stm, start_sector, end_sector - start_sector)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800149 LOG(FATAL, "failed to erase memory\n");
150 }
151
Brian Silvermanb5d83562013-12-18 15:51:41 -0800152 // Read all of the 0xFFs that the parser inserts to pad the data out.
153 {
154 uint8_t garbage[1024];
155 uint32_t length = start_address;
156 while (length > 0) {
157 uint32_t read = ::std::min(sizeof(garbage), length);
158 if (parser->read(p_st, garbage, &read) != PARSER_ERR_OK) {
159 LOG(FATAL, "reading 0xFFs from the hex parser failed\n");
160 }
161 length -= read;
162 }
163 }
164
Brian Silverman2d9cf662013-12-18 14:22:49 -0800165 uint32_t kFlashStart = 0x08000000;
166
167 uint8_t buffer[256]; // 256 is the biggest size supported
168 uint32_t completed = 0;
169 while (completed < size) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800170 uint32_t address = start_address + completed + kFlashStart;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800171 uint32_t length = ::std::min(size - completed, sizeof(buffer));
172 if (parser->read(p_st, buffer, &length) != PARSER_ERR_OK) {
173 LOG(FATAL, "reading file failed\n");
174 }
175 if (length == 0) {
176 LOG(FATAL, "failed to read input file\n");
177 }
Brian Silvermanb5d83562013-12-18 15:51:41 -0800178 if ((length % 4) != 0) {
179 // Pad the size we want to write to a multiple of 4 bytes.
180 for (unsigned int i = 0; i < (4 - (length % 4)); ++i) {
181 buffer[length++] = 0xFF;
182 }
183 }
184 if (!stm32_write_memory(stm, address, buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800185 LOG(FATAL, "failed to write memory at address 0x%x\n", address);
186 }
187 uint8_t compare_buffer[sizeof(buffer)];
Brian Silvermanb5d83562013-12-18 15:51:41 -0800188 if (!stm32_read_memory(stm, address, compare_buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800189 LOG(FATAL, "failed to read memory at address 0x%x\n", address);
190 }
191 if (memcmp(buffer, compare_buffer, length) != 0) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800192 printf("\n");
193 for (size_t i = 0; i < length; ++i) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800194 LOG(DEBUG, "want %x have %x\n", buffer[i], compare_buffer[i]);
Brian Silvermanb5d83562013-12-18 15:51:41 -0800195 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800196 LOG(FATAL, "verify from 0x%x to 0x%x failed\n",
197 address, address + length);
198 }
199 completed += length;
200 printf("\rWrote and verified 0x%08x/0x%08x",
201 completed, size);
202 fflush(stdout);
203 }
204 printf("\n");
205
206 if (init_bl_exit(stm, serial, NULL /* GPIO sequence */)) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800207 LOG(INFO, "all done\n");
Brian Silverman2d9cf662013-12-18 14:22:49 -0800208 } else {
209 LOG(FATAL, "init_bl_exit failed\n");
210 }
211}