blob: ddddb1bda60cf21e545a5befea430dec25f8beb5 [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
Brian Silvermanac5dd402013-12-23 21:50:06 -080031 //::std::string device = "/dev/ttyUSB0";
32 // TODO(brians): Figure out an intelligent way to set the device to use.
33 ::std::string device = "/dev/ttyO1";
Brian Silverman2d9cf662013-12-18 14:22:49 -080034 serial_baud_t baud_rate = SERIAL_BAUD_57600;
35
36 ::std::string filename =
37 ::std::string(dirname(strdup(argv[0]))) +
38 "/../../../bbb_cape/src/cape/.obj/" + target + ".hex";
39
40 int file = open(filename.c_str(), O_RDONLY);
41 if (file == -1) {
Brian Silvermaned030062013-12-20 21:03:47 -080042 filename = target;
43 file = open(filename.c_str(), O_RDONLY);
44 if (file == -1) {
45 LOG(FATAL, "open(%s, O_RDONLY) failed with %d: %s\n",
46 filename.c_str(), errno, strerror(errno));
47 } else {
48 LOG(INFO, "using filename %s from the command line\n", filename.c_str());
49 }
Brian Silverman2d9cf662013-12-18 14:22:49 -080050 }
51
52 uint16_t start_address = 0;
53 {
54 uint8_t buffer[1 /* : */ + 2 /* record size */ + 4 /* address */ +
55 2 /* record type */];
56 ssize_t bytes = read(file, buffer, sizeof(buffer));
57 if (close(file) == -1) {
58 LOG(FATAL, "close(%d) failed with %d: %s\n",
59 file, errno, strerror(errno));
60 }
61 if (bytes != sizeof(buffer)) {
62 LOG(FATAL, "read %zd bytes from %s instead of %zu\n",
63 bytes, filename.c_str(), sizeof(buffer));
64 }
65 if (buffer[0] != ':' || buffer[7] != '0' || buffer[8] != '0') {
66 LOG(FATAL, "%s isn't a valid hex file that we know how to handle\n",
67 filename.c_str());
68 }
69 for (int i = 0; i < 4; ++i) {
70 uint8_t digit = buffer[3 + i];
71 int value;
72 if (digit >= '0' && digit <= '9') {
73 value = digit - '0';
74 } else if (digit >= 'a' && digit <= 'f') {
75 value = digit - 'a';
76 } else if (digit >= 'A' && digit <= 'F') {
77 value = digit - 'A';
78 } else {
79 LOG(FATAL, "unknown hex digit %c\n", digit);
80 }
81 start_address |= value << (12 - (4 * i));
82 }
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080083 LOG(INFO, "start address = 0x%x\n", start_address);
Brian Silverman2d9cf662013-12-18 14:22:49 -080084 }
85
86 parser_t *parser = &PARSER_HEX;
87 void *p_st = parser->init();
88 if (p_st == NULL) {
89 LOG(FATAL, "%s parser failed to initialize.\n", parser->name);
90 }
91 if (parser->open(p_st, filename.c_str(), 0) != PARSER_ERR_OK) {
92 LOG(FATAL, "opening file %s failed\n", filename.c_str());
93 }
94
95 serial_t *serial = serial_open(device.c_str());
96 if (serial == NULL) {
97 LOG(FATAL, "failed to open serial port %s because of %d: %s\n",
98 device.c_str(), errno, strerror(errno));
99 }
100 if (serial_setup(serial, baud_rate,
101 SERIAL_BITS_8,
102 SERIAL_PARITY_EVEN,
103 SERIAL_STOPBIT_1) != SERIAL_ERR_OK) {
104 LOG(FATAL, "setting up serial port %s failed because of %d: %s\n",
105 device.c_str(), errno, strerror(errno));
106 }
107 LOG(INFO, "serial configuration: %s\n", serial_get_setup_str(serial));
108
109 if (init_bl_entry(serial, NULL /* GPIO sequence */) == 0) {
110 LOG(FATAL, "init_bl_entry(%p, NULL) failed\n", serial);
111 }
112 stm32_t *stm = stm32_init(serial, true);
113 if (stm == NULL) {
114 LOG(FATAL, "stm32_init(%p, true) failed\n", serial);
115 }
116
117 unsigned int last_byte = parser->size(p_st);
118 unsigned int size = last_byte - start_address;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800119
120 // An array of the sizes of each sector.
121 static const uint32_t kSectorSizes[12] = {0x4000, 0x4000, 0x4000, 0x4000,
122 0x10000, 0x20000, 0x20000, 0x20000,
123 0x20000, 0x20000, 0x20000, 0x20000};
124 static const int kNumSectors = sizeof(kSectorSizes) / sizeof(kSectorSizes[0]);
125 // The sector number that we're going to start writing at.
126 int start_sector = 0;
127 for (uint32_t address = 0; start_sector <= kNumSectors;
128 address += kSectorSizes[start_sector++]) {
129 if (start_sector == kNumSectors) {
130 LOG(FATAL, "start address %x is too big\n", start_address);
131 }
132 if (address > start_address) {
133 LOG(FATAL, "start address %x is not on a sector boundary\n",
134 start_address);
135 }
136 if (address == start_address) break;
137 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800138
139 // The first sector that we're not going to erase.
140 int end_sector = 0;
141 for (uint32_t address = 0; end_sector <= kNumSectors;
142 address += kSectorSizes[end_sector++]) {
143 if (address > start_address + size) break;
144 if (end_sector == kNumSectors) {
145 LOG(FATAL, "%x bytes beyond start address of %x is too big\n",
146 size, start_address);
147 }
148 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800149
Brian Silvermanb5d83562013-12-18 15:51:41 -0800150 if (!stm32_erase_memory(stm, start_sector, end_sector - start_sector)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800151 LOG(FATAL, "failed to erase memory\n");
152 }
153
Brian Silvermanb5d83562013-12-18 15:51:41 -0800154 // Read all of the 0xFFs that the parser inserts to pad the data out.
155 {
156 uint8_t garbage[1024];
157 uint32_t length = start_address;
158 while (length > 0) {
159 uint32_t read = ::std::min(sizeof(garbage), length);
160 if (parser->read(p_st, garbage, &read) != PARSER_ERR_OK) {
161 LOG(FATAL, "reading 0xFFs from the hex parser failed\n");
162 }
163 length -= read;
164 }
165 }
166
Brian Silverman2d9cf662013-12-18 14:22:49 -0800167 uint32_t kFlashStart = 0x08000000;
168
169 uint8_t buffer[256]; // 256 is the biggest size supported
170 uint32_t completed = 0;
171 while (completed < size) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800172 uint32_t address = start_address + completed + kFlashStart;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800173 uint32_t length = ::std::min(size - completed, sizeof(buffer));
174 if (parser->read(p_st, buffer, &length) != PARSER_ERR_OK) {
175 LOG(FATAL, "reading file failed\n");
176 }
177 if (length == 0) {
178 LOG(FATAL, "failed to read input file\n");
179 }
Brian Silvermanb5d83562013-12-18 15:51:41 -0800180 if ((length % 4) != 0) {
181 // Pad the size we want to write to a multiple of 4 bytes.
182 for (unsigned int i = 0; i < (4 - (length % 4)); ++i) {
183 buffer[length++] = 0xFF;
184 }
185 }
186 if (!stm32_write_memory(stm, address, buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800187 LOG(FATAL, "failed to write memory at address 0x%x\n", address);
188 }
189 uint8_t compare_buffer[sizeof(buffer)];
Brian Silvermanb5d83562013-12-18 15:51:41 -0800190 if (!stm32_read_memory(stm, address, compare_buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800191 LOG(FATAL, "failed to read memory at address 0x%x\n", address);
192 }
193 if (memcmp(buffer, compare_buffer, length) != 0) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800194 printf("\n");
195 for (size_t i = 0; i < length; ++i) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800196 LOG(DEBUG, "want %x have %x\n", buffer[i], compare_buffer[i]);
Brian Silvermanb5d83562013-12-18 15:51:41 -0800197 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800198 LOG(FATAL, "verify from 0x%x to 0x%x failed\n",
199 address, address + length);
200 }
201 completed += length;
202 printf("\rWrote and verified 0x%08x/0x%08x",
203 completed, size);
204 fflush(stdout);
205 }
206 printf("\n");
207
208 if (init_bl_exit(stm, serial, NULL /* GPIO sequence */)) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800209 LOG(INFO, "all done\n");
Brian Silverman2d9cf662013-12-18 14:22:49 -0800210 } else {
211 LOG(FATAL, "init_bl_exit failed\n");
212 }
213}