blob: 639908a1f62b53bfb7e3aa4e2f0efec2a889586c [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"
Brian Silvermane364e382014-02-08 21:51:33 -080011#include "aos/common/time.h"
Brian Silvermanaf784862014-05-13 08:14:55 -070012#include "aos/common/libc/dirname.h"
Brian Silverman2d9cf662013-12-18 14:22:49 -080013
14extern "C" {
15#include "stm32flash/parsers/parser.h"
16#include "stm32flash/parsers/hex.h"
17#include "stm32flash/serial.h"
18#include "stm32flash/stm32.h"
19#include "stm32flash/init.h"
20}
21
Brian Silvermane364e382014-02-08 21:51:33 -080022#include "bbb/gpo.h"
Brian Silverman0117dbc2014-03-24 17:08:38 -070023#include "bbb/export_uart.h"
Brian Silvermane364e382014-02-08 21:51:33 -080024
Brian Silverman8dc9fd42014-02-10 13:35:43 -080025namespace {
26
27void Reset(bool into_bootloader) {
28 ::bbb::Gpo reset(2, 5, true);
29 ::bbb::Gpo bootloader(2, 2, into_bootloader);
30 static constexpr ::aos::time::Time kWaitTime =
31 ::aos::time::Time::InSeconds(0.1);
32 reset.Set(false);
33 ::aos::time::SleepFor(kWaitTime);
34 reset.Set(true);
35 ::aos::time::SleepFor(kWaitTime);
36}
37
38} // namespace
39
Brian Silverman2d9cf662013-12-18 14:22:49 -080040int main(int argc, char **argv) {
41 ::aos::logging::Init();
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080042 ::aos::logging::AddImplementation(
43 new ::aos::logging::StreamLogImplementation(stdout));
Brian Silverman2d9cf662013-12-18 14:22:49 -080044
Brian Silverman8dc9fd42014-02-10 13:35:43 -080045 Reset(true);
Brian Silvermane364e382014-02-08 21:51:33 -080046
Brian Silverman2d9cf662013-12-18 14:22:49 -080047 if (argc < 2) {
48 fputs("Need an argument saying which target to download.\n", stderr);
49 return 1;
50 }
51 ::std::string target = argv[1];
52
Brian Silverman2d9cf662013-12-18 14:22:49 -080053 serial_baud_t baud_rate = SERIAL_BAUD_57600;
54
Brian Silvermanaf784862014-05-13 08:14:55 -070055 ::std::string filename = ::aos::libc::Dirname(argv[0]) +
56 "/../../../bbb_cape/src/cape/.obj/" + target +
57 ".hex";
Brian Silverman2d9cf662013-12-18 14:22:49 -080058
59 int file = open(filename.c_str(), O_RDONLY);
60 if (file == -1) {
Brian Silvermaned030062013-12-20 21:03:47 -080061 filename = target;
62 file = open(filename.c_str(), O_RDONLY);
63 if (file == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070064 PLOG(FATAL, "open(%s, O_RDONLY) failed", filename.c_str());
Brian Silvermaned030062013-12-20 21:03:47 -080065 } else {
66 LOG(INFO, "using filename %s from the command line\n", filename.c_str());
67 }
Brian Silverman2d9cf662013-12-18 14:22:49 -080068 }
69
70 uint16_t start_address = 0;
71 {
72 uint8_t buffer[1 /* : */ + 2 /* record size */ + 4 /* address */ +
73 2 /* record type */];
74 ssize_t bytes = read(file, buffer, sizeof(buffer));
75 if (close(file) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070076 PLOG(FATAL, "close(%d) failed", file);
Brian Silverman2d9cf662013-12-18 14:22:49 -080077 }
78 if (bytes != sizeof(buffer)) {
79 LOG(FATAL, "read %zd bytes from %s instead of %zu\n",
80 bytes, filename.c_str(), sizeof(buffer));
81 }
82 if (buffer[0] != ':' || buffer[7] != '0' || buffer[8] != '0') {
83 LOG(FATAL, "%s isn't a valid hex file that we know how to handle\n",
84 filename.c_str());
85 }
86 for (int i = 0; i < 4; ++i) {
87 uint8_t digit = buffer[3 + i];
88 int value;
89 if (digit >= '0' && digit <= '9') {
90 value = digit - '0';
91 } else if (digit >= 'a' && digit <= 'f') {
92 value = digit - 'a';
93 } else if (digit >= 'A' && digit <= 'F') {
94 value = digit - 'A';
95 } else {
96 LOG(FATAL, "unknown hex digit %c\n", digit);
97 }
98 start_address |= value << (12 - (4 * i));
99 }
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800100 LOG(INFO, "start address = 0x%x\n", start_address);
Brian Silverman2d9cf662013-12-18 14:22:49 -0800101 }
102
103 parser_t *parser = &PARSER_HEX;
104 void *p_st = parser->init();
105 if (p_st == NULL) {
106 LOG(FATAL, "%s parser failed to initialize.\n", parser->name);
107 }
108 if (parser->open(p_st, filename.c_str(), 0) != PARSER_ERR_OK) {
Brian Silverman01be0002014-05-10 15:44:38 -0700109 PLOG(FATAL, "opening file %s failed\n", filename.c_str());
Brian Silverman2d9cf662013-12-18 14:22:49 -0800110 }
111
Brian Silverman0117dbc2014-03-24 17:08:38 -0700112 ::bbb::ExportUart();
113
114 const char *device = ::bbb::UartDevice();
115
116 serial_t *serial = serial_open(device);
Brian Silverman2d9cf662013-12-18 14:22:49 -0800117 if (serial == NULL) {
Brian Silverman01be0002014-05-10 15:44:38 -0700118 PLOG(FATAL, "failed to open serial port %s", device);
Brian Silverman2d9cf662013-12-18 14:22:49 -0800119 }
120 if (serial_setup(serial, baud_rate,
121 SERIAL_BITS_8,
122 SERIAL_PARITY_EVEN,
123 SERIAL_STOPBIT_1) != SERIAL_ERR_OK) {
Brian Silverman01be0002014-05-10 15:44:38 -0700124 PLOG(FATAL, "setting up serial port %s failed", device);
Brian Silverman2d9cf662013-12-18 14:22:49 -0800125 }
126 LOG(INFO, "serial configuration: %s\n", serial_get_setup_str(serial));
127
128 if (init_bl_entry(serial, NULL /* GPIO sequence */) == 0) {
129 LOG(FATAL, "init_bl_entry(%p, NULL) failed\n", serial);
130 }
131 stm32_t *stm = stm32_init(serial, true);
132 if (stm == NULL) {
133 LOG(FATAL, "stm32_init(%p, true) failed\n", serial);
134 }
135
136 unsigned int last_byte = parser->size(p_st);
137 unsigned int size = last_byte - start_address;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800138
139 // An array of the sizes of each sector.
140 static const uint32_t kSectorSizes[12] = {0x4000, 0x4000, 0x4000, 0x4000,
141 0x10000, 0x20000, 0x20000, 0x20000,
142 0x20000, 0x20000, 0x20000, 0x20000};
143 static const int kNumSectors = sizeof(kSectorSizes) / sizeof(kSectorSizes[0]);
144 // The sector number that we're going to start writing at.
145 int start_sector = 0;
146 for (uint32_t address = 0; start_sector <= kNumSectors;
147 address += kSectorSizes[start_sector++]) {
148 if (start_sector == kNumSectors) {
149 LOG(FATAL, "start address %x is too big\n", start_address);
150 }
151 if (address > start_address) {
152 LOG(FATAL, "start address %x is not on a sector boundary\n",
153 start_address);
154 }
155 if (address == start_address) break;
156 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800157
158 // The first sector that we're not going to erase.
159 int end_sector = 0;
160 for (uint32_t address = 0; end_sector <= kNumSectors;
161 address += kSectorSizes[end_sector++]) {
162 if (address > start_address + size) break;
163 if (end_sector == kNumSectors) {
164 LOG(FATAL, "%x bytes beyond start address of %x is too big\n",
165 size, start_address);
166 }
167 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800168
Brian Silvermanb5d83562013-12-18 15:51:41 -0800169 if (!stm32_erase_memory(stm, start_sector, end_sector - start_sector)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800170 LOG(FATAL, "failed to erase memory\n");
171 }
172
Brian Silvermanb5d83562013-12-18 15:51:41 -0800173 // Read all of the 0xFFs that the parser inserts to pad the data out.
174 {
175 uint8_t garbage[1024];
Brian Silverman6208ca82014-02-14 23:17:09 -0800176 size_t length = start_address;
Brian Silvermanb5d83562013-12-18 15:51:41 -0800177 while (length > 0) {
178 uint32_t read = ::std::min(sizeof(garbage), length);
179 if (parser->read(p_st, garbage, &read) != PARSER_ERR_OK) {
180 LOG(FATAL, "reading 0xFFs from the hex parser failed\n");
181 }
182 length -= read;
183 }
184 }
185
Brian Silverman2d9cf662013-12-18 14:22:49 -0800186 uint32_t kFlashStart = 0x08000000;
187
188 uint8_t buffer[256]; // 256 is the biggest size supported
Brian Silverman6208ca82014-02-14 23:17:09 -0800189 size_t completed = 0;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800190 while (completed < size) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800191 uint32_t address = start_address + completed + kFlashStart;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800192 uint32_t length = ::std::min(size - completed, sizeof(buffer));
193 if (parser->read(p_st, buffer, &length) != PARSER_ERR_OK) {
194 LOG(FATAL, "reading file failed\n");
195 }
196 if (length == 0) {
197 LOG(FATAL, "failed to read input file\n");
198 }
Brian Silvermanb5d83562013-12-18 15:51:41 -0800199 if ((length % 4) != 0) {
200 // Pad the size we want to write to a multiple of 4 bytes.
201 for (unsigned int i = 0; i < (4 - (length % 4)); ++i) {
202 buffer[length++] = 0xFF;
203 }
204 }
205 if (!stm32_write_memory(stm, address, buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800206 LOG(FATAL, "failed to write memory at address 0x%x\n", address);
207 }
208 uint8_t compare_buffer[sizeof(buffer)];
Brian Silvermanb5d83562013-12-18 15:51:41 -0800209 if (!stm32_read_memory(stm, address, compare_buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800210 LOG(FATAL, "failed to read memory at address 0x%x\n", address);
211 }
212 if (memcmp(buffer, compare_buffer, length) != 0) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800213 printf("\n");
214 for (size_t i = 0; i < length; ++i) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800215 LOG(DEBUG, "want %x have %x\n", buffer[i], compare_buffer[i]);
Brian Silvermanb5d83562013-12-18 15:51:41 -0800216 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800217 LOG(FATAL, "verify from 0x%x to 0x%x failed\n",
218 address, address + length);
219 }
220 completed += length;
Brian Silverman6208ca82014-02-14 23:17:09 -0800221 printf("\rWrote and verified 0x%08zx/0x%08x",
Brian Silverman2d9cf662013-12-18 14:22:49 -0800222 completed, size);
223 fflush(stdout);
224 }
225 printf("\n");
226
227 if (init_bl_exit(stm, serial, NULL /* GPIO sequence */)) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800228 LOG(INFO, "all done\n");
Brian Silverman8dc9fd42014-02-10 13:35:43 -0800229 Reset(false);
Brian Silverman2d9cf662013-12-18 14:22:49 -0800230 } else {
231 LOG(FATAL, "init_bl_exit failed\n");
232 }
233}