blob: 0f081f5783359b36d5e5f82699177e4e9a48fd94 [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 Silverman2d9cf662013-12-18 14:22:49 -080023int main(int argc, char **argv) {
24 ::aos::logging::Init();
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080025 ::aos::logging::AddImplementation(
26 new ::aos::logging::StreamLogImplementation(stdout));
Brian Silverman2d9cf662013-12-18 14:22:49 -080027
Brian Silvermane364e382014-02-08 21:51:33 -080028 {
29 ::bbb::Gpo reset(2, 5, true);
30 ::bbb::Gpo bootloader(2, 2, false);
31 static constexpr ::aos::time::Time kWaitTime =
32 ::aos::time::Time::InSeconds(0.1);
33 reset.Set(false);
34 ::aos::time::SleepFor(kWaitTime);
35 bootloader.Set(false);
36 ::aos::time::SleepFor(kWaitTime);
37 reset.Set(true);
38 ::aos::time::SleepFor(kWaitTime);
39 }
40
Brian Silverman2d9cf662013-12-18 14:22:49 -080041 if (argc < 2) {
42 fputs("Need an argument saying which target to download.\n", stderr);
43 return 1;
44 }
45 ::std::string target = argv[1];
46
Brian Silvermanac5dd402013-12-23 21:50:06 -080047 //::std::string device = "/dev/ttyUSB0";
48 // TODO(brians): Figure out an intelligent way to set the device to use.
49 ::std::string device = "/dev/ttyO1";
Brian Silverman2d9cf662013-12-18 14:22:49 -080050 serial_baud_t baud_rate = SERIAL_BAUD_57600;
51
52 ::std::string filename =
53 ::std::string(dirname(strdup(argv[0]))) +
54 "/../../../bbb_cape/src/cape/.obj/" + target + ".hex";
55
56 int file = open(filename.c_str(), O_RDONLY);
57 if (file == -1) {
Brian Silvermaned030062013-12-20 21:03:47 -080058 filename = target;
59 file = open(filename.c_str(), O_RDONLY);
60 if (file == -1) {
61 LOG(FATAL, "open(%s, O_RDONLY) failed with %d: %s\n",
62 filename.c_str(), errno, strerror(errno));
63 } else {
64 LOG(INFO, "using filename %s from the command line\n", filename.c_str());
65 }
Brian Silverman2d9cf662013-12-18 14:22:49 -080066 }
67
68 uint16_t start_address = 0;
69 {
70 uint8_t buffer[1 /* : */ + 2 /* record size */ + 4 /* address */ +
71 2 /* record type */];
72 ssize_t bytes = read(file, buffer, sizeof(buffer));
73 if (close(file) == -1) {
74 LOG(FATAL, "close(%d) failed with %d: %s\n",
75 file, errno, strerror(errno));
76 }
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) {
108 LOG(FATAL, "opening file %s failed\n", filename.c_str());
109 }
110
111 serial_t *serial = serial_open(device.c_str());
112 if (serial == NULL) {
113 LOG(FATAL, "failed to open serial port %s because of %d: %s\n",
114 device.c_str(), errno, strerror(errno));
115 }
116 if (serial_setup(serial, baud_rate,
117 SERIAL_BITS_8,
118 SERIAL_PARITY_EVEN,
119 SERIAL_STOPBIT_1) != SERIAL_ERR_OK) {
120 LOG(FATAL, "setting up serial port %s failed because of %d: %s\n",
121 device.c_str(), errno, strerror(errno));
122 }
123 LOG(INFO, "serial configuration: %s\n", serial_get_setup_str(serial));
124
125 if (init_bl_entry(serial, NULL /* GPIO sequence */) == 0) {
126 LOG(FATAL, "init_bl_entry(%p, NULL) failed\n", serial);
127 }
128 stm32_t *stm = stm32_init(serial, true);
129 if (stm == NULL) {
130 LOG(FATAL, "stm32_init(%p, true) failed\n", serial);
131 }
132
133 unsigned int last_byte = parser->size(p_st);
134 unsigned int size = last_byte - start_address;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800135
136 // An array of the sizes of each sector.
137 static const uint32_t kSectorSizes[12] = {0x4000, 0x4000, 0x4000, 0x4000,
138 0x10000, 0x20000, 0x20000, 0x20000,
139 0x20000, 0x20000, 0x20000, 0x20000};
140 static const int kNumSectors = sizeof(kSectorSizes) / sizeof(kSectorSizes[0]);
141 // The sector number that we're going to start writing at.
142 int start_sector = 0;
143 for (uint32_t address = 0; start_sector <= kNumSectors;
144 address += kSectorSizes[start_sector++]) {
145 if (start_sector == kNumSectors) {
146 LOG(FATAL, "start address %x is too big\n", start_address);
147 }
148 if (address > start_address) {
149 LOG(FATAL, "start address %x is not on a sector boundary\n",
150 start_address);
151 }
152 if (address == start_address) break;
153 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800154
155 // The first sector that we're not going to erase.
156 int end_sector = 0;
157 for (uint32_t address = 0; end_sector <= kNumSectors;
158 address += kSectorSizes[end_sector++]) {
159 if (address > start_address + size) break;
160 if (end_sector == kNumSectors) {
161 LOG(FATAL, "%x bytes beyond start address of %x is too big\n",
162 size, start_address);
163 }
164 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800165
Brian Silvermanb5d83562013-12-18 15:51:41 -0800166 if (!stm32_erase_memory(stm, start_sector, end_sector - start_sector)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800167 LOG(FATAL, "failed to erase memory\n");
168 }
169
Brian Silvermanb5d83562013-12-18 15:51:41 -0800170 // Read all of the 0xFFs that the parser inserts to pad the data out.
171 {
172 uint8_t garbage[1024];
173 uint32_t length = start_address;
174 while (length > 0) {
175 uint32_t read = ::std::min(sizeof(garbage), length);
176 if (parser->read(p_st, garbage, &read) != PARSER_ERR_OK) {
177 LOG(FATAL, "reading 0xFFs from the hex parser failed\n");
178 }
179 length -= read;
180 }
181 }
182
Brian Silverman2d9cf662013-12-18 14:22:49 -0800183 uint32_t kFlashStart = 0x08000000;
184
185 uint8_t buffer[256]; // 256 is the biggest size supported
186 uint32_t completed = 0;
187 while (completed < size) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800188 uint32_t address = start_address + completed + kFlashStart;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800189 uint32_t length = ::std::min(size - completed, sizeof(buffer));
190 if (parser->read(p_st, buffer, &length) != PARSER_ERR_OK) {
191 LOG(FATAL, "reading file failed\n");
192 }
193 if (length == 0) {
194 LOG(FATAL, "failed to read input file\n");
195 }
Brian Silvermanb5d83562013-12-18 15:51:41 -0800196 if ((length % 4) != 0) {
197 // Pad the size we want to write to a multiple of 4 bytes.
198 for (unsigned int i = 0; i < (4 - (length % 4)); ++i) {
199 buffer[length++] = 0xFF;
200 }
201 }
202 if (!stm32_write_memory(stm, address, buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800203 LOG(FATAL, "failed to write memory at address 0x%x\n", address);
204 }
205 uint8_t compare_buffer[sizeof(buffer)];
Brian Silvermanb5d83562013-12-18 15:51:41 -0800206 if (!stm32_read_memory(stm, address, compare_buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800207 LOG(FATAL, "failed to read memory at address 0x%x\n", address);
208 }
209 if (memcmp(buffer, compare_buffer, length) != 0) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800210 printf("\n");
211 for (size_t i = 0; i < length; ++i) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800212 LOG(DEBUG, "want %x have %x\n", buffer[i], compare_buffer[i]);
Brian Silvermanb5d83562013-12-18 15:51:41 -0800213 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800214 LOG(FATAL, "verify from 0x%x to 0x%x failed\n",
215 address, address + length);
216 }
217 completed += length;
218 printf("\rWrote and verified 0x%08x/0x%08x",
219 completed, size);
220 fflush(stdout);
221 }
222 printf("\n");
223
224 if (init_bl_exit(stm, serial, NULL /* GPIO sequence */)) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800225 LOG(INFO, "all done\n");
Brian Silverman2d9cf662013-12-18 14:22:49 -0800226 } else {
227 LOG(FATAL, "init_bl_exit failed\n");
228 }
229}