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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
/*
* This file is part of the bayou project.
*
* Copyright (C) 2008 Advanced Micro Devices, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include "liblar.h"
#include "self.h"
static int lar_compress(struct LAR *lar, int algo, char *src, char *dst,
int len)
{
int ret;
if (!lar->cfuncs[algo])
return -1;
lar->cfuncs[algo] (src, len, dst, &ret);
return ret;
}
static int lar_decompress(struct LAR *lar, int algo, char *src, char *dst,
int slen, int dlen)
{
if (!lar->dfuncs[algo])
return -1;
lar->dfuncs[algo] (src, slen, dst, dlen);
return dlen;
}
static struct LARHeader *lar_get_header(struct LAR *lar, const char *filename)
{
char *buffer;
int offset = 0;
struct LARHeader *lheader = NULL;
struct lar_header *header;
printf("Getting %s\n", filename);
buffer = malloc(sizeof(struct lar_header) + MAX_PATHLEN);
if (buffer == NULL)
return NULL;
while (1) {
int ret;
if (lseek(lar->fd, offset, SEEK_SET) == -1)
goto err;
ret = read(lar->fd, buffer, sizeof(struct lar_header));
if (ret <= 0)
goto err;
header = (struct lar_header *)buffer;
if (strncmp(header->magic, MAGIC, sizeof(MAGIC)))
goto err;
ret = read(lar->fd, buffer + sizeof(struct lar_header),
ntohl(header->offset) - sizeof(struct lar_header));
if (ret <= 0)
goto err;
if (!strcmp(buffer + sizeof(struct lar_header), filename))
break;
offset += ntohl(header->offset) +
((ntohl(header->len) + 15) & ~0xF);
}
lheader = calloc(sizeof(struct LARHeader), 1);
if (lheader == NULL)
goto err;
lheader->hoffset = offset;
lheader->offset = offset + ntohl(header->offset);
lheader->reallen = ntohl(header->reallen);
lheader->len = ntohl(header->len);
lheader->loadaddress = ntohl(header->loadaddress);
lheader->compression = ntohl(header->compression);
lheader->entry = ntohl(header->entry);
lheader->checksum = ntohl(header->checksum);
err:
free(buffer);
return lheader;
}
static int LAR_AppendBlob(struct LAR *lar, unsigned char *buffer,
int len, int rlen, struct LARAttr *attr)
{
int nlen, nsize, lsize, i;
struct lar_header *header;
u8 *lptr;
u32 csum = 0;
if (attr == NULL)
return -1;
nlen = strlen(attr->name) + 1;
if (nlen > MAX_PATHLEN - 1)
nlen = MAX_PATHLEN - 1;
nsize = (nlen + 15) & ~0xF;
lsize = sizeof(struct lar_header) + nsize + len;
lptr = calloc(lsize + 1, 1);
if (lptr == NULL)
return -1;
header = (struct lar_header *)lptr;
memcpy(header->magic, MAGIC, 8);
header->reallen = htonl(rlen);
header->len = htonl(len);
header->offset = htonl(lsize - len);
header->loadaddress = htonl(attr->loadaddr);
header->compression = htonl(attr->compression);
header->entry = htonl(attr->entry);
strncpy(((char *)header) + sizeof(struct lar_header), attr->name, nlen);
for (i = 0; i < sizeof(struct lar_header) + nsize; i += 4)
csum += *((u32 *) (lptr + i));
for (i = 0; i < len; i += 4) {
/*
* The checksum needs to include the 16 byte padding at
* the end of the data before the next lar header. The
* problem is that the padding isn't going to be in the
* buffer, and if we try to read off the end of the buffer,
* we are just asking for trouble. So account for the
* situation where the datalen is not a multiple of four
* and get a safe value to add into the checksum.
* The rest of the padding will be zero, and can be safely
* ignored here.
*/
if ((len - i) < 4) {
u32 val = 0;
int t;
for (t = 0; t < (len - i); t++)
val |= *((u8 *) buffer + (i + t)) << (t * 8);
csum += val;
} else
csum += *((u32 *) (buffer + i));
}
header->checksum = (u32) (~0 - csum);
lseek(lar->fd, 0, SEEK_END);
/* FIXME: Error check here. */
write(lar->fd, header, sizeof(struct lar_header) + nsize);
write(lar->fd, buffer, len);
/* Add in padding to the next 16 byte boundary. */
if (lsize & 0xF) {
int i;
char null = '\0';
for (i = lsize & 0xF; i < 0x10; i++)
write(lar->fd, &null, 1);
}
return 0;
}
int LAR_AppendBuffer(struct LAR *lar, unsigned char *buffer, int len,
struct LARAttr *attr)
{
unsigned char *tbuf;
int rlen, ret = -1;
if (attr->compression == ALGO_NONE)
return LAR_AppendBlob(lar, buffer, len, len, attr);
tbuf = malloc(len);
if (tbuf == NULL)
return -1;
rlen = lar_compress(lar, attr->compression, (char *)buffer,
(char *)tbuf, len);
if (rlen > 0)
ret = LAR_AppendBlob(lar, tbuf, rlen, len, attr);
free(tbuf);
return ret;
}
int LAR_AppendSelf(struct LAR *lar, const char *filename, struct LARAttr *attr)
{
unsigned char *buffer;
int len = elf_to_self(filename, &buffer,
lar->cfuncs[attr->compression]);
int ret;
if (len == -1)
return -1;
ret = LAR_AppendBlob(lar, buffer, len, len, attr);
free(buffer);
return ret;
}
int LAR_AppendFile(struct LAR *lar, const char *filename, struct LARAttr *attr)
{
int fd;
struct stat s;
char *filep;
int ret;
if (iself((char *)filename))
return LAR_AppendSelf(lar, filename, attr);
fd = open(filename, O_RDONLY);
if (fd == -1)
return -1;
if (fstat(fd, &s))
return -1;
filep = (char *)mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (filep == MAP_FAILED)
return -1;
ret = LAR_AppendBuffer(lar, (unsigned char *)filep, s.st_size, attr);
munmap(filep, s.st_size);
return ret;
}
int LAR_DeleteFile(struct LAR *lar, const char *filename)
{
struct LARHeader *header = lar_get_header(lar, filename);
int len, ret = -1;
char *filep, *buffer;
if (header == NULL)
return -1;
buffer = malloc(4096);
if (buffer == NULL)
return -1;
len = header->offset + header->len;
/* First, map the space and zero it out. */
filep = (char *)mmap(0, len, PROT_READ, MAP_SHARED, lar->fd,
header->hoffset);
if (filep == MAP_FAILED)
return -1;
memset(filep, 0, len);
munmap(filep, len);
/* Now move the rest of the LAR into place. */
/* FIXME: This does not account for the bootblock! */
int dst = header->hoffset;
int src = header->hoffset + len;
while (1) {
int l, w;
if (lseek(lar->fd, src, SEEK_SET))
goto err;
l = read(lar->fd, buffer, 8192);
if (l == -1)
goto err;
if (l == 0)
goto err;
if (lseek(lar->fd, dst, SEEK_SET))
goto err;
w = write(lar->fd, buffer, l);
if (w <= 0)
goto err;
dst += w;
src += w;
}
ret = 0;
err:
free(buffer);
return ret;
}
void LAR_CloseFile(struct LARFile *file)
{
if (file != NULL) {
if (file->buffer != NULL)
free(file->buffer);
free(file);
}
}
struct LARFile *LAR_MapFile(struct LAR *lar, const char *filename)
{
struct LARFile *file;
struct LARHeader *header = lar_get_header(lar, filename);
char *filep;
int ret;
if (header == NULL)
return NULL;
file = calloc(sizeof(struct LARFile), 1);
if (file == NULL)
return NULL;
file->len = header->reallen;
file->buffer = calloc(header->reallen, 1);
if (file->buffer == NULL)
goto err;
/*
* The offset needs to be a multiple of PAGE_SIZE, so just mmap
* from offset 0, its easier then doing the math.
*/
filep = mmap(0, header->offset + header->len,
PROT_READ, MAP_SHARED, lar->fd, 0);
if (filep == MAP_FAILED) {
printf("Map failed: %m\n");
goto err;
}
if (header->compression != ALGO_NONE) {
ret = lar_decompress(lar, header->compression,
filep + header->offset, file->buffer,
header->len, header->reallen);
} else {
memcpy(file->buffer, filep + header->offset, header->len);
ret = header->len;
}
munmap(filep, header->offset + header->len);
if (ret == header->reallen)
return file;
err:
if (file->buffer)
free(file->buffer);
free(file);
return NULL;
}
int LAR_SetCompressionFuncs(struct LAR *lar, int algo,
LAR_CompFunc cfunc, LAR_DecompFunc dfunc)
{
if (algo >= ALGO_INVALID)
return -1;
lar->cfuncs[algo] = cfunc;
lar->dfuncs[algo] = dfunc;
return 0;
}
void LAR_Close(struct LAR *lar)
{
if (lar != NULL) {
if (lar->fd)
close(lar->fd);
free(lar);
}
}
struct LAR *LAR_Open(const char *filename)
{
struct LAR *lar = calloc(sizeof(struct LAR), 1);
if (lar == NULL)
return NULL;
lar->fd = open(filename, O_RDWR);
if (lar->fd > 0)
return lar;
free(lar);
return NULL;
}
struct LAR *LAR_Create(const char *filename)
{
struct LAR *lar = calloc(sizeof(struct LAR), 1);
if (lar == NULL)
return NULL;
lar->fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (lar->fd > 0)
return lar;
free(lar);
return NULL;
}
void LAR_SetAttrs(struct LARAttr *attrs, char *name, int algo)
{
if (attrs == NULL)
return;
memset(attrs, 0, sizeof(*attrs));
snprintf(attrs->name, sizeof(attrs->name) - 1, name);
attrs->compression = algo;
}
|