forked from nPipen/lpcpatchelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlpcpatchelf.c
268 lines (232 loc) · 7.39 KB
/
lpcpatchelf.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* File : lpcpatchelf.c
*
* Description: Utility to compute and patch the correct flash image
* checksum into LPC17xx (and other LPC microcontrollers)
*
* Author : Nils Pipenbrinck <[email protected]>
* Copyright : (C) Nils Pipenbrinck 2016
* Website : http://hilbert-space.de/?p=178
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <libelf.h>
#include <stdbool.h>
#include <getopt.h>
#define VERSION_MAJ 1
#define VERSION_MIN 0
#define NUMVECTORS 8
static uint32_t get_lpc_signature (const uint32_t *sig,
int NumVectors,
int CheckVector)
//////////////////////////////////////////////////////////////
{
uint32_t cksum = 0;
for (int i = 0; i < NumVectors; i++)
{
if (i != CheckVector)
cksum += sig[i];
}
return 0-cksum;
}
bool check_arm (Elf *e)
///////////////////////
{
Elf32_Ehdr * hdr = elf32_getehdr (e);
if (!hdr)
{
fprintf (stderr, "elf32_getehdr failed %s\n", elf_errmsg(elf_errno()));
return false;
}
if (hdr->e_machine != EM_ARM)
{
fprintf (stderr, "Sorry, this is not an ARM-binary\n");
return false;
}
return true;
}
bool patch_elf (const char * filename, int CheckVector)
////////////////////////////////////////////////////////
{
bool patch_success = false;
if (elf_version(EV_CURRENT) == EV_NONE)
{
fprintf (stderr, "ELF library too old\n");
return false;
}
int fd = open (filename, O_RDWR);
if (fd <0)
{
fprintf (stderr, "unable to open file %s\n", filename);
return false;
}
Elf *e = elf_begin(fd, ELF_C_RDWR, (Elf *) 0);
if (!e)
{
fprintf (stderr, "elf_begin failed %s\n", elf_errmsg(elf_errno()));
return false;
}
// We don't want libelf to mess with our layout in any way!
// libelf may break arm binaries otherwise.
if (!elf_flagelf (e, ELF_C_SET, ELF_F_LAYOUT))
{
fprintf (stderr, "elf_flagelf failed %s\n", elf_errmsg(elf_errno()));
return false;
}
if (!check_arm(e))
{
return false;
}
// find the code-section:
Elf_Scn * section = 0;
while ((section = elf_nextscn(e, section)) != 0)
{
Elf32_Shdr * hdr = elf32_getshdr (section);
if (!hdr)
{
fprintf (stderr, "elf32_getshdr failed %s\n", elf_errmsg(elf_errno()));
return false;
}
if ( (hdr->sh_flags & SHF_ALLOC) &&
(hdr->sh_flags & SHF_EXECINSTR) &&
(hdr->sh_addr == 0) &&
(hdr->sh_size >= NUMVECTORS*sizeof(uint32_t)))
{
Elf_Data *data = elf_getdata (section, 0);
if (!data->d_buf)
{
fprintf (stderr, "executable section seems to be empty\n");
return false;
}
uint32_t signature[NUMVECTORS];
memcpy (signature, data->d_buf, NUMVECTORS * sizeof (uint32_t));
uint32_t cksum = get_lpc_signature(signature, NUMVECTORS, CheckVector);
printf ("old checksum: %08x\n", signature[CheckVector]);
printf ("new checksum: %08x\n", cksum);
signature[CheckVector] = cksum;
// tag data-chunk as dirty. This forces recalculation of the elf checksum.
if (!elf_flagdata (data, ELF_C_SET, ELF_F_DIRTY))
{
fprintf (stderr, "elf_flagdata failed %s\n", elf_errmsg(elf_errno()));
return false;
}
// update ELF-file:
memcpy (data->d_buf, signature, NUMVECTORS * sizeof (uint32_t));
if (elf_update(e, ELF_C_WRITE) == -1)
{
fprintf (stderr, "elf_update failed %s\n", elf_errmsg(elf_errno()));
return false;
}
// we're done:
patch_success = true;
break;
}
}
elf_end (e);
close (fd);
return patch_success;
}
void help ()
////////////
{
printf ("Usage: lpcpatchelf -f file.elf [-c PositionOfChecksum]\n");
printf ("\n");
printf (" PositionOfChecksum:\n");
printf ("\n");
printf (" The -c option tells where to place the checksum into the interrupt\n");
printf (" table. Default is 7 for LPC17xx and most other LPC microcontrollers\n");
printf (" for the LPC2000 family use uses position 5\n");
printf ("\n");
printf ("\n");
printf ("lpcpatchelf - Updates LPC ARM processor specific checksum in elf-binaries.\n");
printf ("\n");
printf ("Copyright (C) 2016 Nils Pipenbrinck Email: [email protected]\n");
printf ("Version %d.%d, compiled %s\n", VERSION_MAJ, VERSION_MIN, __DATE__);
printf ("\n");
printf ("This program is free software; you can redistribute it and/or\n");
printf ("modify it under the terms of the GNU General Public License\n");
printf ("as published by the Free Software Foundation; either version 2\n");
printf ("of the License, or (at your option) any later version.\n");
}
int main (int argc, char **argv)
/////////////////////////////////
{
const char * elf_file = 0;
int c;
// Defaults for LPC17xx and LPC43xx familiy
int CheckVector = 7;
while ((c = getopt (argc, argv, "f:n:c:")) != -1)
{
switch (c)
{
case 'f':
elf_file = optarg;
break;
case 'c':
CheckVector = atol(optarg);
break;
case '?':
if ((optopt == 'f') || (optopt == 'c'))
{
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
}
else if (isprint (optopt))
{
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
}
else
{
fprintf (stderr,"Unknown option character `\\x%x'.\n", optopt);
}
help();
return EXIT_FAILURE;
default:
help();
abort ();
}
}
if ((CheckVector <0) || (CheckVector >= NUMVECTORS))
{
fprintf (stderr, "illegal PositionOfChecksum value\n");
return EXIT_FAILURE;
}
if (elf_file)
{
if (patch_elf(elf_file, CheckVector))
{
return EXIT_SUCCESS;
}
else
{
fprintf (stderr, "something failed, probably you've passed an .elf file that this program doesn't understand\n");
return EXIT_FAILURE;
}
}
else
{
help();
return EXIT_FAILURE;
}
}
/*EOF */