blob: 73f778c4ec147265b7932d09f99e876371478f8c [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
31 ::std::string device = "/dev/ttyUSB0";
32 serial_baud_t baud_rate = SERIAL_BAUD_57600;
33
34 ::std::string filename =
35 ::std::string(dirname(strdup(argv[0]))) +
36 "/../../../bbb_cape/src/cape/.obj/" + target + ".hex";
37
38 int file = open(filename.c_str(), O_RDONLY);
39 if (file == -1) {
40 LOG(FATAL, "open(%s, O_RDONLY) failed with %d: %s\n",
41 filename.c_str(), errno, strerror(errno));
42 }
43
44 uint16_t start_address = 0;
45 {
46 uint8_t buffer[1 /* : */ + 2 /* record size */ + 4 /* address */ +
47 2 /* record type */];
48 ssize_t bytes = read(file, buffer, sizeof(buffer));
49 if (close(file) == -1) {
50 LOG(FATAL, "close(%d) failed with %d: %s\n",
51 file, errno, strerror(errno));
52 }
53 if (bytes != sizeof(buffer)) {
54 LOG(FATAL, "read %zd bytes from %s instead of %zu\n",
55 bytes, filename.c_str(), sizeof(buffer));
56 }
57 if (buffer[0] != ':' || buffer[7] != '0' || buffer[8] != '0') {
58 LOG(FATAL, "%s isn't a valid hex file that we know how to handle\n",
59 filename.c_str());
60 }
61 for (int i = 0; i < 4; ++i) {
62 uint8_t digit = buffer[3 + i];
63 int value;
64 if (digit >= '0' && digit <= '9') {
65 value = digit - '0';
66 } else if (digit >= 'a' && digit <= 'f') {
67 value = digit - 'a';
68 } else if (digit >= 'A' && digit <= 'F') {
69 value = digit - 'A';
70 } else {
71 LOG(FATAL, "unknown hex digit %c\n", digit);
72 }
73 start_address |= value << (12 - (4 * i));
74 }
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080075 LOG(INFO, "start address = 0x%x\n", start_address);
Brian Silverman2d9cf662013-12-18 14:22:49 -080076 }
77
78 parser_t *parser = &PARSER_HEX;
79 void *p_st = parser->init();
80 if (p_st == NULL) {
81 LOG(FATAL, "%s parser failed to initialize.\n", parser->name);
82 }
83 if (parser->open(p_st, filename.c_str(), 0) != PARSER_ERR_OK) {
84 LOG(FATAL, "opening file %s failed\n", filename.c_str());
85 }
86
87 serial_t *serial = serial_open(device.c_str());
88 if (serial == NULL) {
89 LOG(FATAL, "failed to open serial port %s because of %d: %s\n",
90 device.c_str(), errno, strerror(errno));
91 }
92 if (serial_setup(serial, baud_rate,
93 SERIAL_BITS_8,
94 SERIAL_PARITY_EVEN,
95 SERIAL_STOPBIT_1) != SERIAL_ERR_OK) {
96 LOG(FATAL, "setting up serial port %s failed because of %d: %s\n",
97 device.c_str(), errno, strerror(errno));
98 }
99 LOG(INFO, "serial configuration: %s\n", serial_get_setup_str(serial));
100
101 if (init_bl_entry(serial, NULL /* GPIO sequence */) == 0) {
102 LOG(FATAL, "init_bl_entry(%p, NULL) failed\n", serial);
103 }
104 stm32_t *stm = stm32_init(serial, true);
105 if (stm == NULL) {
106 LOG(FATAL, "stm32_init(%p, true) failed\n", serial);
107 }
108
109 unsigned int last_byte = parser->size(p_st);
110 unsigned int size = last_byte - start_address;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800111
112 // An array of the sizes of each sector.
113 static const uint32_t kSectorSizes[12] = {0x4000, 0x4000, 0x4000, 0x4000,
114 0x10000, 0x20000, 0x20000, 0x20000,
115 0x20000, 0x20000, 0x20000, 0x20000};
116 static const int kNumSectors = sizeof(kSectorSizes) / sizeof(kSectorSizes[0]);
117 // The sector number that we're going to start writing at.
118 int start_sector = 0;
119 for (uint32_t address = 0; start_sector <= kNumSectors;
120 address += kSectorSizes[start_sector++]) {
121 if (start_sector == kNumSectors) {
122 LOG(FATAL, "start address %x is too big\n", start_address);
123 }
124 if (address > start_address) {
125 LOG(FATAL, "start address %x is not on a sector boundary\n",
126 start_address);
127 }
128 if (address == start_address) break;
129 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800130
131 // The first sector that we're not going to erase.
132 int end_sector = 0;
133 for (uint32_t address = 0; end_sector <= kNumSectors;
134 address += kSectorSizes[end_sector++]) {
135 if (address > start_address + size) break;
136 if (end_sector == kNumSectors) {
137 LOG(FATAL, "%x bytes beyond start address of %x is too big\n",
138 size, start_address);
139 }
140 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800141
Brian Silvermanb5d83562013-12-18 15:51:41 -0800142 if (!stm32_erase_memory(stm, start_sector, end_sector - start_sector)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800143 LOG(FATAL, "failed to erase memory\n");
144 }
145
Brian Silvermanb5d83562013-12-18 15:51:41 -0800146 // Read all of the 0xFFs that the parser inserts to pad the data out.
147 {
148 uint8_t garbage[1024];
149 uint32_t length = start_address;
150 while (length > 0) {
151 uint32_t read = ::std::min(sizeof(garbage), length);
152 if (parser->read(p_st, garbage, &read) != PARSER_ERR_OK) {
153 LOG(FATAL, "reading 0xFFs from the hex parser failed\n");
154 }
155 length -= read;
156 }
157 }
158
Brian Silverman2d9cf662013-12-18 14:22:49 -0800159 uint32_t kFlashStart = 0x08000000;
160
161 uint8_t buffer[256]; // 256 is the biggest size supported
162 uint32_t completed = 0;
163 while (completed < size) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800164 uint32_t address = start_address + completed + kFlashStart;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800165 uint32_t length = ::std::min(size - completed, sizeof(buffer));
166 if (parser->read(p_st, buffer, &length) != PARSER_ERR_OK) {
167 LOG(FATAL, "reading file failed\n");
168 }
169 if (length == 0) {
170 LOG(FATAL, "failed to read input file\n");
171 }
Brian Silvermanb5d83562013-12-18 15:51:41 -0800172 if ((length % 4) != 0) {
173 // Pad the size we want to write to a multiple of 4 bytes.
174 for (unsigned int i = 0; i < (4 - (length % 4)); ++i) {
175 buffer[length++] = 0xFF;
176 }
177 }
178 if (!stm32_write_memory(stm, address, buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800179 LOG(FATAL, "failed to write memory at address 0x%x\n", address);
180 }
181 uint8_t compare_buffer[sizeof(buffer)];
Brian Silvermanb5d83562013-12-18 15:51:41 -0800182 if (!stm32_read_memory(stm, address, compare_buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800183 LOG(FATAL, "failed to read memory at address 0x%x\n", address);
184 }
185 if (memcmp(buffer, compare_buffer, length) != 0) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800186 printf("\n");
187 for (size_t i = 0; i < length; ++i) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800188 LOG(DEBUG, "want %x have %x\n", buffer[i], compare_buffer[i]);
Brian Silvermanb5d83562013-12-18 15:51:41 -0800189 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800190 LOG(FATAL, "verify from 0x%x to 0x%x failed\n",
191 address, address + length);
192 }
193 completed += length;
194 printf("\rWrote and verified 0x%08x/0x%08x",
195 completed, size);
196 fflush(stdout);
197 }
198 printf("\n");
199
200 if (init_bl_exit(stm, serial, NULL /* GPIO sequence */)) {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800201 LOG(INFO, "all done\n");
Brian Silverman2d9cf662013-12-18 14:22:49 -0800202 } else {
203 LOG(FATAL, "init_bl_exit failed\n");
204 }
205}