-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
209 lines (177 loc) · 6.25 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "sdo.h"
#include "lpc.h"
#include "firmware.h"
#include "progressbar.h"
can_t can;
size_t current_sector = 0;
char *current_action = "downloading to ram";
static uint8_t magic_reset_bytes[]
= {0x80, 0xB5, 0x00, 0xAF, 0xBF, 0xF3, 0x4F, 0x8F, 0x02, 0x4B, 0x03, 0x4A, 0xDA, 0x60, 0xBF, 0xF3, 0x4F, 0x8F, 0xFE, 0xE7, 0x00, 0xED, 0x00, 0xE0, 0x04, 0x00, 0xFA, 0x05};
void progress_print(int max, int current)
{
char tmp[50];
sprintf(tmp, "sector %zu", current_sector);
print_progress(max, current, tmp, current_action);
}
int main(int argc, char *argv[])
{
uint32_t err;
// command line arguments
if (argc < 3)
{
printf("invalid arguments: ./lpc_can_flash <interface> <filename>\n");
exit(-1);
}
// load the firmware file
size_t sector_count;
sector_t *sectors = firmware_open(argv[2], LPC_11C24_SECTOR_SIZE, §or_count);
if (sectors == 0)
{
printf("failed to open firmware file \"%s\"\n", argv[2]);
exit(-1);
}
// print the anatomy of the firmware file and stats
printf("------------------------------------------------------------\n");
printf("firmware file %s:\n", argv[2]);
printf("------------------------------------------------------------\n");
printf("file anatomy:\n");
size_t firmware_size = 0;
for (int i = 0; i < sector_count; i++)
{
printf("\tsector %d, size: %zu, addr: 0x%04x - 0x%04zx\n", i, sectors[i].size, i*LPC_11C24_SECTOR_SIZE, (i*LPC_11C24_SECTOR_SIZE + sectors[i].size - 1));
firmware_size += sectors[i].size;
}
printf("used space: %zu bytes, free space: %zu byte\n", firmware_size, (LPC_FLASH_SIZE - firmware_size));
// check firmware plausibility
if (firmware_size <= LPC_FLASH_SIZE && sector_count <= LPC_11C24_SECTOR_COUT)
{
printf("firmware ok, download is possible\n");
} else {
printf("error: firmware does not fit into flash\n");
exit(-1);
}
// start the download process
printf("\n------------------------------------------------------------\n");
printf("firmware download:\n");
printf("------------------------------------------------------------\n");
// initialize the can interface
printf("opening CAN interface: ...");
err = can_init(&can, argv[1]);
if (err != CAN_SUCCESS)
{
printf("\ropenining CAN interface: failed [%s]\n", strerror(err));
exit(-1);
}
printf("\ropening CAN interface: done\n");
// verify the lpc processors part id
printf("verifying lpc part id: ...");
fflush(stdout);
uint32_t lpc_part_id = 0;
err = sdo_upload(&can, LPC_SDO_NODE_ID, LPC_SDO_IDENTITY_IDX, LPC_SDO_PARTID_SUBIDX, &lpc_part_id);
if (lpc_part_id == 0x1430102B)
{
printf("\rverifying lpc part id: [OK] %s\n", lpc_part_name(lpc_part_id));
} else {
printf("\rverifying lpc part id: [FAILED] %s\n", lpc_part_name(lpc_part_id));
printf("err: %d, %x\n", err, lpc_part_id);
exit(-1);
}
// send unlock code
printf("sending unlock code: ...");
uint16_t code = LPC_UNLOCK_CODE;
sdo_download_exp(&can, LPC_SDO_NODE_ID, LPC_SDO_UNLOCK_IDX, LPC_SDO_UNLOCK_SUBIDX, (uint8_t*)&code, 2);
printf("\rsending unlock code: done\n");
// erase all sectors
printf("erasing sector 0-7: ...");
lpc_erase_sector(&can, 0, 7);
printf("\rerasing sector 0-7: done\n\n");
// gather some information we need later
const uint32_t ram_start_addr = lpc_ram_addr(&can);
// write the firmware sector by sector to the uC
for (int i = 0; i < sector_count; i++)
{
// number of bytes to write to flash
uint16_t bytes_to_copy = 0;
// download the sectors content
err = sdo_download_buffer(&can, LPC_SDO_NODE_ID, 0x1F50, 0x01, sectors[i].binary, sectors[i].size, progress_print);
if (err != CAN_SUCCESS)
{
printf("\nerror in download_sector_payload: 0x%x\n", err);
exit(-1);
}
// this is a full sector -> straight-forward copy
if (sectors[i].size == LPC_11C24_SECTOR_SIZE)
{
bytes_to_copy = sectors[i].size;
// not a full sector, we have to fill with zeroes util next 2^n size
} else {
current_action = "filling unused ram";
// calculate the next matching block size and fill with 0xFF
const size_t next_block_size = lpc_next_sector_size(sectors[i].size);
size_t zero_count = next_block_size - sectors[i].size;
uint8_t zeroes[zero_count];
memset(zeroes, 0xFF, zero_count);
err = sdo_download_buffer(&can, LPC_SDO_NODE_ID, 0x1F50, 0x01, zeroes, zero_count, progress_print);
if (err != CAN_SUCCESS) {
printf("\nerror in download_zeroes: 0x%x\n", err);
exit(-1);
}
bytes_to_copy = next_block_size;
}
// prepare the sector and copy to flash
char tmp[50];
sprintf(tmp, "sector %zu", current_sector);
print_progress(1, 1, tmp, "copy to flash");
lpc_prepare_sector(&can, i, i);
err = sdo_download_exp(&can, LPC_SDO_NODE_ID, 0x5050, 0x03, (uint8_t*)&bytes_to_copy, 2);
if (err != CAN_SUCCESS)
{
printf("\nerror in copy_to_flash: 0x%x [count: %d]\n", err, bytes_to_copy);
exit(-1);
}
// reset ram address and set next flash addr
sdo_download_exp(&can, LPC_SDO_NODE_ID, LPC_SDO_RAM_ADDR_IDX, 0x00, (uint8_t*)&ram_start_addr, 4);
uint32_t next_flash_addr = (i + 1) * LPC_11C24_SECTOR_SIZE;
err = sdo_download_exp(&can, LPC_SDO_NODE_ID, 0x5050, 0x01, (uint8_t*)&next_flash_addr, 4);
if (err != CAN_SUCCESS)
{
printf("\nerror in rest_ram_addr: 0x%x\n", err);
exit(-1);
}
// increment the sector number for the progressbar
current_sector++;
current_action = "downloading to ram";
printf("\n");
}
printf("firmware download finished\n");
// write the magic reset bytes to ram
printf("resetting microcontroller: ...");
err = sdo_download_exp(&can, LPC_SDO_NODE_ID, LPC_SDO_RAM_ADDR_IDX, 0x00, (uint8_t*)&ram_start_addr, 4);
if (err != CAN_SUCCESS)
{
printf("\rresetting microcontroller: [FAILED] error setting ramaddress 0x%x\n", err);
exit(-1);
}
err = sdo_download_buffer(&can, LPC_SDO_NODE_ID, 0x1F50, 0x01, magic_reset_bytes, sizeof(magic_reset_bytes), NULL);
if (err != CAN_SUCCESS)
{
printf("\rresetting microcontroller: [FAILED] error downloading magic bytes\n");
exit(-1);
}
// execute the magic reset bytes
err = lpc_go(&can, ram_start_addr);
if (err != CAN_SUCCESS)
{
printf("\rresetting microcontroller: [FAILED] go failed 0x%x\n", err);
exit(-1);
}
printf("\rresetting microcontroller: [OK]\n");
// close
firmware_free(sectors);
can_close(&can);
return 0;
}