blob: 646657b98b33c35c1d67899f4ad1ea5f913a344d [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();
22
23 if (argc < 2) {
24 fputs("Need an argument saying which target to download.\n", stderr);
25 return 1;
26 }
27 ::std::string target = argv[1];
28
29 ::std::string device = "/dev/ttyUSB0";
30 serial_baud_t baud_rate = SERIAL_BAUD_57600;
31
32 ::std::string filename =
33 ::std::string(dirname(strdup(argv[0]))) +
34 "/../../../bbb_cape/src/cape/.obj/" + target + ".hex";
35
36 int file = open(filename.c_str(), O_RDONLY);
37 if (file == -1) {
38 LOG(FATAL, "open(%s, O_RDONLY) failed with %d: %s\n",
39 filename.c_str(), errno, strerror(errno));
40 }
41
42 uint16_t start_address = 0;
43 {
44 uint8_t buffer[1 /* : */ + 2 /* record size */ + 4 /* address */ +
45 2 /* record type */];
46 ssize_t bytes = read(file, buffer, sizeof(buffer));
47 if (close(file) == -1) {
48 LOG(FATAL, "close(%d) failed with %d: %s\n",
49 file, errno, strerror(errno));
50 }
51 if (bytes != sizeof(buffer)) {
52 LOG(FATAL, "read %zd bytes from %s instead of %zu\n",
53 bytes, filename.c_str(), sizeof(buffer));
54 }
55 if (buffer[0] != ':' || buffer[7] != '0' || buffer[8] != '0') {
56 LOG(FATAL, "%s isn't a valid hex file that we know how to handle\n",
57 filename.c_str());
58 }
59 for (int i = 0; i < 4; ++i) {
60 uint8_t digit = buffer[3 + i];
61 int value;
62 if (digit >= '0' && digit <= '9') {
63 value = digit - '0';
64 } else if (digit >= 'a' && digit <= 'f') {
65 value = digit - 'a';
66 } else if (digit >= 'A' && digit <= 'F') {
67 value = digit - 'A';
68 } else {
69 LOG(FATAL, "unknown hex digit %c\n", digit);
70 }
71 start_address |= value << (12 - (4 * i));
72 }
73 printf("%x\n", start_address);
74 }
75
76 parser_t *parser = &PARSER_HEX;
77 void *p_st = parser->init();
78 if (p_st == NULL) {
79 LOG(FATAL, "%s parser failed to initialize.\n", parser->name);
80 }
81 if (parser->open(p_st, filename.c_str(), 0) != PARSER_ERR_OK) {
82 LOG(FATAL, "opening file %s failed\n", filename.c_str());
83 }
84
85 serial_t *serial = serial_open(device.c_str());
86 if (serial == NULL) {
87 LOG(FATAL, "failed to open serial port %s because of %d: %s\n",
88 device.c_str(), errno, strerror(errno));
89 }
90 if (serial_setup(serial, baud_rate,
91 SERIAL_BITS_8,
92 SERIAL_PARITY_EVEN,
93 SERIAL_STOPBIT_1) != SERIAL_ERR_OK) {
94 LOG(FATAL, "setting up serial port %s failed because of %d: %s\n",
95 device.c_str(), errno, strerror(errno));
96 }
97 LOG(INFO, "serial configuration: %s\n", serial_get_setup_str(serial));
98
99 if (init_bl_entry(serial, NULL /* GPIO sequence */) == 0) {
100 LOG(FATAL, "init_bl_entry(%p, NULL) failed\n", serial);
101 }
102 stm32_t *stm = stm32_init(serial, true);
103 if (stm == NULL) {
104 LOG(FATAL, "stm32_init(%p, true) failed\n", serial);
105 }
106
107 unsigned int last_byte = parser->size(p_st);
108 unsigned int size = last_byte - start_address;
109 printf("%x\n", size);
110
111 // An array of the sizes of each sector.
112 static const uint32_t kSectorSizes[12] = {0x4000, 0x4000, 0x4000, 0x4000,
113 0x10000, 0x20000, 0x20000, 0x20000,
114 0x20000, 0x20000, 0x20000, 0x20000};
115 static const int kNumSectors = sizeof(kSectorSizes) / sizeof(kSectorSizes[0]);
116 // The sector number that we're going to start writing at.
117 int start_sector = 0;
118 for (uint32_t address = 0; start_sector <= kNumSectors;
119 address += kSectorSizes[start_sector++]) {
120 if (start_sector == kNumSectors) {
121 LOG(FATAL, "start address %x is too big\n", start_address);
122 }
123 if (address > start_address) {
124 LOG(FATAL, "start address %x is not on a sector boundary\n",
125 start_address);
126 }
127 if (address == start_address) break;
128 }
129 printf("starting with sector %d\n", start_sector);
130
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 }
141 printf("not erasing sector %d\n", end_sector);
142
Brian Silvermanb5d83562013-12-18 15:51:41 -0800143 if (!stm32_erase_memory(stm, start_sector, end_sector - start_sector)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800144 LOG(FATAL, "failed to erase memory\n");
145 }
146
Brian Silvermanb5d83562013-12-18 15:51:41 -0800147 // Read all of the 0xFFs that the parser inserts to pad the data out.
148 {
149 uint8_t garbage[1024];
150 uint32_t length = start_address;
151 while (length > 0) {
152 uint32_t read = ::std::min(sizeof(garbage), length);
153 if (parser->read(p_st, garbage, &read) != PARSER_ERR_OK) {
154 LOG(FATAL, "reading 0xFFs from the hex parser failed\n");
155 }
156 length -= read;
157 }
158 }
159
Brian Silverman2d9cf662013-12-18 14:22:49 -0800160 uint32_t kFlashStart = 0x08000000;
161
162 uint8_t buffer[256]; // 256 is the biggest size supported
163 uint32_t completed = 0;
164 while (completed < size) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800165 uint32_t address = start_address + completed + kFlashStart;
Brian Silverman2d9cf662013-12-18 14:22:49 -0800166 uint32_t length = ::std::min(size - completed, sizeof(buffer));
167 if (parser->read(p_st, buffer, &length) != PARSER_ERR_OK) {
168 LOG(FATAL, "reading file failed\n");
169 }
170 if (length == 0) {
171 LOG(FATAL, "failed to read input file\n");
172 }
Brian Silvermanb5d83562013-12-18 15:51:41 -0800173 if ((length % 4) != 0) {
174 // Pad the size we want to write to a multiple of 4 bytes.
175 for (unsigned int i = 0; i < (4 - (length % 4)); ++i) {
176 buffer[length++] = 0xFF;
177 }
178 }
179 if (!stm32_write_memory(stm, address, buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800180 LOG(FATAL, "failed to write memory at address 0x%x\n", address);
181 }
182 uint8_t compare_buffer[sizeof(buffer)];
Brian Silvermanb5d83562013-12-18 15:51:41 -0800183 if (!stm32_read_memory(stm, address, compare_buffer, length)) {
Brian Silverman2d9cf662013-12-18 14:22:49 -0800184 LOG(FATAL, "failed to read memory at address 0x%x\n", address);
185 }
186 if (memcmp(buffer, compare_buffer, length) != 0) {
Brian Silvermanb5d83562013-12-18 15:51:41 -0800187 printf("\n");
188 for (size_t i = 0; i < length; ++i) {
189 printf("want %x have %x\n", buffer[i], compare_buffer[i]);
190 }
Brian Silverman2d9cf662013-12-18 14:22:49 -0800191 LOG(FATAL, "verify from 0x%x to 0x%x failed\n",
192 address, address + length);
193 }
194 completed += length;
195 printf("\rWrote and verified 0x%08x/0x%08x",
196 completed, size);
197 fflush(stdout);
198 }
199 printf("\n");
200
201 if (init_bl_exit(stm, serial, NULL /* GPIO sequence */)) {
202 printf("done!\n");
203 } else {
204 LOG(FATAL, "init_bl_exit failed\n");
205 }
206}