blob: c2e8bf2c3002d200f1dfb91cd71a292f35dada43 [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"
22
Brian Silverman8dc9fd42014-02-10 13:35:43 -080023namespace {
24
25void Reset(bool into_bootloader) {
26 ::bbb::Gpo reset(2, 5, true);
27 ::bbb::Gpo bootloader(2, 2, into_bootloader);
28 static constexpr ::aos::time::Time kWaitTime =
29 ::aos::time::Time::InSeconds(0.1);
30 reset.Set(false);
31 ::aos::time::SleepFor(kWaitTime);
32 reset.Set(true);
33 ::aos::time::SleepFor(kWaitTime);
34}
35
36} // namespace
37
Brian Silverman2d9cf662013-12-18 14:22:49 -080038int main(int argc, char **argv) {
39 ::aos::logging::Init();
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080040 ::aos::logging::AddImplementation(
41 new ::aos::logging::StreamLogImplementation(stdout));
Brian Silverman2d9cf662013-12-18 14:22:49 -080042
Brian Silverman8dc9fd42014-02-10 13:35:43 -080043 Reset(true);
Brian Silvermane364e382014-02-08 21:51:33 -080044
Brian Silverman2d9cf662013-12-18 14:22:49 -080045 if (argc < 2) {
46 fputs("Need an argument saying which target to download.\n", stderr);
47 return 1;
48 }
49 ::std::string target = argv[1];
50
Brian Silvermanac5dd402013-12-23 21:50:06 -080051 //::std::string device = "/dev/ttyUSB0";
52 // TODO(brians): Figure out an intelligent way to set the device to use.
53 ::std::string device = "/dev/ttyO1";
Brian Silverman2d9cf662013-12-18 14:22:49 -080054 serial_baud_t baud_rate = SERIAL_BAUD_57600;
55
56 ::std::string filename =
57 ::std::string(dirname(strdup(argv[0]))) +
58 "/../../../bbb_cape/src/cape/.obj/" + target + ".hex";
59
60 int file = open(filename.c_str(), O_RDONLY);
61 if (file == -1) {
Brian Silvermaned030062013-12-20 21:03:47 -080062 filename = target;
63 file = open(filename.c_str(), O_RDONLY);
64 if (file == -1) {
65 LOG(FATAL, "open(%s, O_RDONLY) failed with %d: %s\n",
66 filename.c_str(), errno, strerror(errno));
67 } else {
68 LOG(INFO, "using filename %s from the command line\n", filename.c_str());
69 }
Brian Silverman2d9cf662013-12-18 14:22:49 -080070 }
71
72 uint16_t start_address = 0;
73 {
74 uint8_t buffer[1 /* : */ + 2 /* record size */ + 4 /* address */ +
75 2 /* record type */];
76 ssize_t bytes = read(file, buffer, sizeof(buffer));
77 if (close(file) == -1) {
78 LOG(FATAL, "close(%d) failed with %d: %s\n",
79 file, errno, strerror(errno));
80 }
81 if (bytes != sizeof(buffer)) {
82 LOG(FATAL, "read %zd bytes from %s instead of %zu\n",
83 bytes, filename.c_str(), sizeof(buffer));
84 }
85 if (buffer[0] != ':' || buffer[7] != '0' || buffer[8] != '0') {
86 LOG(FATAL, "%s isn't a valid hex file that we know how to handle\n",
87 filename.c_str());
88 }
89 for (int i = 0; i < 4; ++i) {
90 uint8_t digit = buffer[3 + i];
91 int value;
92 if (digit >= '0' && digit <= '9') {
93 value = digit - '0';
94 } else if (digit >= 'a' && digit <= 'f') {
95 value = digit - 'a';
96 } else if (digit >= 'A' && digit <= 'F') {
97 value = digit - 'A';
98 } else {
99 LOG(FATAL, "unknown hex digit %c\n", digit);
100 }
101 start_address |= value << (12 - (4 * i));
102 }
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800103 LOG(INFO, "start address = 0x%x\n", start_address);
Brian Silverman2d9cf662013-12-18 14:22:49 -0800104 }
105
106 parser_t *parser = &PARSER_HEX;
107 void *p_st = parser->init();
108 if (p_st == NULL) {
109 LOG(FATAL, "%s parser failed to initialize.\n", parser->name);
110 }
111 if (parser->open(p_st, filename.c_str(), 0) != PARSER_ERR_OK) {
112 LOG(FATAL, "opening file %s failed\n", filename.c_str());
113 }
114
115 serial_t *serial = serial_open(device.c_str());
116 if (serial == NULL) {
117 LOG(FATAL, "failed to open serial port %s because of %d: %s\n",
118 device.c_str(), errno, strerror(errno));
119 }
120 if (serial_setup(serial, baud_rate,
121 SERIAL_BITS_8,
122 SERIAL_PARITY_EVEN,
123 SERIAL_STOPBIT_1) != SERIAL_ERR_OK) {
124 LOG(FATAL, "setting up serial port %s failed because of %d: %s\n",
125 device.c_str(), errno, strerror(errno));
126 }
127 LOG(INFO, "serial configuration: %s\n", serial_get_setup_str(serial));
128
129 if (init_bl_entry(serial, NULL /* GPIO sequence */) == 0) {
130 LOG(FATAL, "init_bl_entry(%p, NULL) failed\n", serial);
131 }
132 stm32_t *stm = stm32_init(serial, true);
133 if (stm == NULL) {
134 LOG(FATAL, "stm32_init(%p, true) failed\n", serial);
135 }
136
137 unsigned int last_byte = parser->size(p_st);
138 unsigned int size = last_byte - start_address;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800139
140 // An array of the sizes of each sector.
141 static const uint32_t kSectorSizes[12] = {0x4000, 0x4000, 0x4000, 0x4000,
142 0x10000, 0x20000, 0x20000, 0x20000,
143 0x20000, 0x20000, 0x20000, 0x20000};
144 static const int kNumSectors = sizeof(kSectorSizes) / sizeof(kSectorSizes[0]);
145 // The sector number that we're going to start writing at.
146 int start_sector = 0;
147 for (uint32_t address = 0; start_sector <= kNumSectors;
148 address += kSectorSizes[start_sector++]) {
149 if (start_sector == kNumSectors) {
150 LOG(FATAL, "start address %x is too big\n", start_address);
151 }
152 if (address > start_address) {
153 LOG(FATAL, "start address %x is not on a sector boundary\n",
154 start_address);
155 }
156 if (address == start_address) break;
157 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800158
159 // The first sector that we're not going to erase.
160 int end_sector = 0;
161 for (uint32_t address = 0; end_sector <= kNumSectors;
162 address += kSectorSizes[end_sector++]) {
163 if (address > start_address + size) break;
164 if (end_sector == kNumSectors) {
165 LOG(FATAL, "%x bytes beyond start address of %x is too big\n",
166 size, start_address);
167 }
168 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800169
Brian Silvermanb5d83562013-12-18 15:51:41 -0800170 if (!stm32_erase_memory(stm, start_sector, end_sector - start_sector)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800171 LOG(FATAL, "failed to erase memory\n");
172 }
173
Brian Silvermanb5d83562013-12-18 15:51:41 -0800174 // Read all of the 0xFFs that the parser inserts to pad the data out.
175 {
176 uint8_t garbage[1024];
177 uint32_t length = start_address;
178 while (length > 0) {
179 uint32_t read = ::std::min(sizeof(garbage), length);
180 if (parser->read(p_st, garbage, &read) != PARSER_ERR_OK) {
181 LOG(FATAL, "reading 0xFFs from the hex parser failed\n");
182 }
183 length -= read;
184 }
185 }
186
Brian Silverman2d9cf662013-12-18 14:22:49 -0800187 uint32_t kFlashStart = 0x08000000;
188
189 uint8_t buffer[256]; // 256 is the biggest size supported
190 uint32_t completed = 0;
191 while (completed < size) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800192 uint32_t address = start_address + completed + kFlashStart;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800193 uint32_t length = ::std::min(size - completed, sizeof(buffer));
194 if (parser->read(p_st, buffer, &length) != PARSER_ERR_OK) {
195 LOG(FATAL, "reading file failed\n");
196 }
197 if (length == 0) {
198 LOG(FATAL, "failed to read input file\n");
199 }
Brian Silvermanb5d83562013-12-18 15:51:41 -0800200 if ((length % 4) != 0) {
201 // Pad the size we want to write to a multiple of 4 bytes.
202 for (unsigned int i = 0; i < (4 - (length % 4)); ++i) {
203 buffer[length++] = 0xFF;
204 }
205 }
206 if (!stm32_write_memory(stm, address, buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800207 LOG(FATAL, "failed to write memory at address 0x%x\n", address);
208 }
209 uint8_t compare_buffer[sizeof(buffer)];
Brian Silvermanb5d83562013-12-18 15:51:41 -0800210 if (!stm32_read_memory(stm, address, compare_buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800211 LOG(FATAL, "failed to read memory at address 0x%x\n", address);
212 }
213 if (memcmp(buffer, compare_buffer, length) != 0) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800214 printf("\n");
215 for (size_t i = 0; i < length; ++i) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800216 LOG(DEBUG, "want %x have %x\n", buffer[i], compare_buffer[i]);
Brian Silvermanb5d83562013-12-18 15:51:41 -0800217 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800218 LOG(FATAL, "verify from 0x%x to 0x%x failed\n",
219 address, address + length);
220 }
221 completed += length;
222 printf("\rWrote and verified 0x%08x/0x%08x",
223 completed, size);
224 fflush(stdout);
225 }
226 printf("\n");
227
228 if (init_bl_exit(stm, serial, NULL /* GPIO sequence */)) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800229 LOG(INFO, "all done\n");
Brian Silverman8dc9fd42014-02-10 13:35:43 -0800230 Reset(false);
Brian Silverman2d9cf662013-12-18 14:22:49 -0800231 } else {
232 LOG(FATAL, "init_bl_exit failed\n");
233 }
234}