blob: d9d87fe463c29cb33fd8af7b2ec439ea1af4c30a [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 Silverman2d9cf662013-12-18 14:22:49 -080012
13extern "C" {
14#include "stm32flash/parsers/parser.h"
15#include "stm32flash/parsers/hex.h"
16#include "stm32flash/serial.h"
17#include "stm32flash/stm32.h"
18#include "stm32flash/init.h"
19}
20
Brian Silvermane364e382014-02-08 21:51:33 -080021#include "bbb/gpo.h"
Brian Silverman0117dbc2014-03-24 17:08:38 -070022#include "bbb/export_uart.h"
Brian Silvermane364e382014-02-08 21:51:33 -080023
Brian Silverman8dc9fd42014-02-10 13:35:43 -080024namespace {
25
26void Reset(bool into_bootloader) {
27 ::bbb::Gpo reset(2, 5, true);
28 ::bbb::Gpo bootloader(2, 2, into_bootloader);
29 static constexpr ::aos::time::Time kWaitTime =
30 ::aos::time::Time::InSeconds(0.1);
31 reset.Set(false);
32 ::aos::time::SleepFor(kWaitTime);
33 reset.Set(true);
34 ::aos::time::SleepFor(kWaitTime);
35}
36
37} // namespace
38
Brian Silverman2d9cf662013-12-18 14:22:49 -080039int main(int argc, char **argv) {
40 ::aos::logging::Init();
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080041 ::aos::logging::AddImplementation(
42 new ::aos::logging::StreamLogImplementation(stdout));
Brian Silverman2d9cf662013-12-18 14:22:49 -080043
Brian Silverman8dc9fd42014-02-10 13:35:43 -080044 Reset(true);
Brian Silvermane364e382014-02-08 21:51:33 -080045
Brian Silverman2d9cf662013-12-18 14:22:49 -080046 if (argc < 2) {
47 fputs("Need an argument saying which target to download.\n", stderr);
48 return 1;
49 }
50 ::std::string target = argv[1];
51
Brian Silverman2d9cf662013-12-18 14:22:49 -080052 serial_baud_t baud_rate = SERIAL_BAUD_57600;
53
54 ::std::string filename =
55 ::std::string(dirname(strdup(argv[0]))) +
56 "/../../../bbb_cape/src/cape/.obj/" + target + ".hex";
57
58 int file = open(filename.c_str(), O_RDONLY);
59 if (file == -1) {
Brian Silvermaned030062013-12-20 21:03:47 -080060 filename = target;
61 file = open(filename.c_str(), O_RDONLY);
62 if (file == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070063 PLOG(FATAL, "open(%s, O_RDONLY) failed", filename.c_str());
Brian Silvermaned030062013-12-20 21:03:47 -080064 } else {
65 LOG(INFO, "using filename %s from the command line\n", filename.c_str());
66 }
Brian Silverman2d9cf662013-12-18 14:22:49 -080067 }
68
69 uint16_t start_address = 0;
70 {
71 uint8_t buffer[1 /* : */ + 2 /* record size */ + 4 /* address */ +
72 2 /* record type */];
73 ssize_t bytes = read(file, buffer, sizeof(buffer));
74 if (close(file) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -070075 PLOG(FATAL, "close(%d) failed", file);
Brian Silverman2d9cf662013-12-18 14:22:49 -080076 }
77 if (bytes != sizeof(buffer)) {
78 LOG(FATAL, "read %zd bytes from %s instead of %zu\n",
79 bytes, filename.c_str(), sizeof(buffer));
80 }
81 if (buffer[0] != ':' || buffer[7] != '0' || buffer[8] != '0') {
82 LOG(FATAL, "%s isn't a valid hex file that we know how to handle\n",
83 filename.c_str());
84 }
85 for (int i = 0; i < 4; ++i) {
86 uint8_t digit = buffer[3 + i];
87 int value;
88 if (digit >= '0' && digit <= '9') {
89 value = digit - '0';
90 } else if (digit >= 'a' && digit <= 'f') {
91 value = digit - 'a';
92 } else if (digit >= 'A' && digit <= 'F') {
93 value = digit - 'A';
94 } else {
95 LOG(FATAL, "unknown hex digit %c\n", digit);
96 }
97 start_address |= value << (12 - (4 * i));
98 }
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080099 LOG(INFO, "start address = 0x%x\n", start_address);
Brian Silverman2d9cf662013-12-18 14:22:49 -0800100 }
101
102 parser_t *parser = &PARSER_HEX;
103 void *p_st = parser->init();
104 if (p_st == NULL) {
105 LOG(FATAL, "%s parser failed to initialize.\n", parser->name);
106 }
107 if (parser->open(p_st, filename.c_str(), 0) != PARSER_ERR_OK) {
Brian Silverman01be0002014-05-10 15:44:38 -0700108 PLOG(FATAL, "opening file %s failed\n", filename.c_str());
Brian Silverman2d9cf662013-12-18 14:22:49 -0800109 }
110
Brian Silverman0117dbc2014-03-24 17:08:38 -0700111 ::bbb::ExportUart();
112
113 const char *device = ::bbb::UartDevice();
114
115 serial_t *serial = serial_open(device);
Brian Silverman2d9cf662013-12-18 14:22:49 -0800116 if (serial == NULL) {
Brian Silverman01be0002014-05-10 15:44:38 -0700117 PLOG(FATAL, "failed to open serial port %s", device);
Brian Silverman2d9cf662013-12-18 14:22:49 -0800118 }
119 if (serial_setup(serial, baud_rate,
120 SERIAL_BITS_8,
121 SERIAL_PARITY_EVEN,
122 SERIAL_STOPBIT_1) != SERIAL_ERR_OK) {
Brian Silverman01be0002014-05-10 15:44:38 -0700123 PLOG(FATAL, "setting up serial port %s failed", device);
Brian Silverman2d9cf662013-12-18 14:22:49 -0800124 }
125 LOG(INFO, "serial configuration: %s\n", serial_get_setup_str(serial));
126
127 if (init_bl_entry(serial, NULL /* GPIO sequence */) == 0) {
128 LOG(FATAL, "init_bl_entry(%p, NULL) failed\n", serial);
129 }
130 stm32_t *stm = stm32_init(serial, true);
131 if (stm == NULL) {
132 LOG(FATAL, "stm32_init(%p, true) failed\n", serial);
133 }
134
135 unsigned int last_byte = parser->size(p_st);
136 unsigned int size = last_byte - start_address;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800137
138 // An array of the sizes of each sector.
139 static const uint32_t kSectorSizes[12] = {0x4000, 0x4000, 0x4000, 0x4000,
140 0x10000, 0x20000, 0x20000, 0x20000,
141 0x20000, 0x20000, 0x20000, 0x20000};
142 static const int kNumSectors = sizeof(kSectorSizes) / sizeof(kSectorSizes[0]);
143 // The sector number that we're going to start writing at.
144 int start_sector = 0;
145 for (uint32_t address = 0; start_sector <= kNumSectors;
146 address += kSectorSizes[start_sector++]) {
147 if (start_sector == kNumSectors) {
148 LOG(FATAL, "start address %x is too big\n", start_address);
149 }
150 if (address > start_address) {
151 LOG(FATAL, "start address %x is not on a sector boundary\n",
152 start_address);
153 }
154 if (address == start_address) break;
155 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800156
157 // The first sector that we're not going to erase.
158 int end_sector = 0;
159 for (uint32_t address = 0; end_sector <= kNumSectors;
160 address += kSectorSizes[end_sector++]) {
161 if (address > start_address + size) break;
162 if (end_sector == kNumSectors) {
163 LOG(FATAL, "%x bytes beyond start address of %x is too big\n",
164 size, start_address);
165 }
166 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800167
Brian Silvermanb5d83562013-12-18 15:51:41 -0800168 if (!stm32_erase_memory(stm, start_sector, end_sector - start_sector)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800169 LOG(FATAL, "failed to erase memory\n");
170 }
171
Brian Silvermanb5d83562013-12-18 15:51:41 -0800172 // Read all of the 0xFFs that the parser inserts to pad the data out.
173 {
174 uint8_t garbage[1024];
Brian Silverman6208ca82014-02-14 23:17:09 -0800175 size_t length = start_address;
Brian Silvermanb5d83562013-12-18 15:51:41 -0800176 while (length > 0) {
177 uint32_t read = ::std::min(sizeof(garbage), length);
178 if (parser->read(p_st, garbage, &read) != PARSER_ERR_OK) {
179 LOG(FATAL, "reading 0xFFs from the hex parser failed\n");
180 }
181 length -= read;
182 }
183 }
184
Brian Silverman2d9cf662013-12-18 14:22:49 -0800185 uint32_t kFlashStart = 0x08000000;
186
187 uint8_t buffer[256]; // 256 is the biggest size supported
Brian Silverman6208ca82014-02-14 23:17:09 -0800188 size_t completed = 0;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800189 while (completed < size) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800190 uint32_t address = start_address + completed + kFlashStart;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800191 uint32_t length = ::std::min(size - completed, sizeof(buffer));
192 if (parser->read(p_st, buffer, &length) != PARSER_ERR_OK) {
193 LOG(FATAL, "reading file failed\n");
194 }
195 if (length == 0) {
196 LOG(FATAL, "failed to read input file\n");
197 }
Brian Silvermanb5d83562013-12-18 15:51:41 -0800198 if ((length % 4) != 0) {
199 // Pad the size we want to write to a multiple of 4 bytes.
200 for (unsigned int i = 0; i < (4 - (length % 4)); ++i) {
201 buffer[length++] = 0xFF;
202 }
203 }
204 if (!stm32_write_memory(stm, address, buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800205 LOG(FATAL, "failed to write memory at address 0x%x\n", address);
206 }
207 uint8_t compare_buffer[sizeof(buffer)];
Brian Silvermanb5d83562013-12-18 15:51:41 -0800208 if (!stm32_read_memory(stm, address, compare_buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800209 LOG(FATAL, "failed to read memory at address 0x%x\n", address);
210 }
211 if (memcmp(buffer, compare_buffer, length) != 0) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800212 printf("\n");
213 for (size_t i = 0; i < length; ++i) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800214 LOG(DEBUG, "want %x have %x\n", buffer[i], compare_buffer[i]);
Brian Silvermanb5d83562013-12-18 15:51:41 -0800215 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800216 LOG(FATAL, "verify from 0x%x to 0x%x failed\n",
217 address, address + length);
218 }
219 completed += length;
Brian Silverman6208ca82014-02-14 23:17:09 -0800220 printf("\rWrote and verified 0x%08zx/0x%08x",
Brian Silverman2d9cf662013-12-18 14:22:49 -0800221 completed, size);
222 fflush(stdout);
223 }
224 printf("\n");
225
226 if (init_bl_exit(stm, serial, NULL /* GPIO sequence */)) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800227 LOG(INFO, "all done\n");
Brian Silverman8dc9fd42014-02-10 13:35:43 -0800228 Reset(false);
Brian Silverman2d9cf662013-12-18 14:22:49 -0800229 } else {
230 LOG(FATAL, "init_bl_exit failed\n");
231 }
232}