blob: 68efd0e2154c060be146ad502c6d2f889dc11f4a [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
143 if (!stm32_erase_memory(stm, start_sector,
144 end_sector - start_sector - 1 /* you send the
145 bootloader N-1 */)) {
146 LOG(FATAL, "failed to erase memory\n");
147 }
148
149 uint32_t kFlashStart = 0x08000000;
150
151 uint8_t buffer[256]; // 256 is the biggest size supported
152 uint32_t completed = 0;
153 while (completed < size) {
154 uint32_t address = start_address + completed;
155 uint32_t length = ::std::min(size - completed, sizeof(buffer));
156 if (parser->read(p_st, buffer, &length) != PARSER_ERR_OK) {
157 LOG(FATAL, "reading file failed\n");
158 }
159 if (length == 0) {
160 LOG(FATAL, "failed to read input file\n");
161 }
162 if (!stm32_write_memory(stm, kFlashStart + address, buffer, length)) {
163 LOG(FATAL, "failed to write memory at address 0x%x\n", address);
164 }
165 uint8_t compare_buffer[sizeof(buffer)];
166 if (!stm32_read_memory(stm, kFlashStart + address, compare_buffer,
167 length)) {
168 LOG(FATAL, "failed to read memory at address 0x%x\n", address);
169 }
170 if (memcmp(buffer, compare_buffer, length) != 0) {
171 LOG(FATAL, "verify from 0x%x to 0x%x failed\n",
172 address, address + length);
173 }
174 completed += length;
175 printf("\rWrote and verified 0x%08x/0x%08x",
176 completed, size);
177 fflush(stdout);
178 }
179 printf("\n");
180
181 if (init_bl_exit(stm, serial, NULL /* GPIO sequence */)) {
182 printf("done!\n");
183 } else {
184 LOG(FATAL, "init_bl_exit failed\n");
185 }
186}