summaryrefslogtreecommitdiff
path: root/ami_slab.c
blob: 07a6e1dbfff172219ec53eb927d1b2af0d7343b8 (plain)
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
/*
 * Copyright 2010      Michael Karcher <flashrom@mkarcher.dialup.fu-berlin.de>
 *
 * 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, 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; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <sys/mman.h>

#if !defined(le32toh) || !defined(le16toh)
#if BYTE_ORDER == LITTLE_ENDIAN
#define le32toh(x) (x)
#define le16toh(x) (x)
#else
#include <byteswap.h>
#define le32toh(x) bswap_32(x)
#define le16toh(x) bswap_16(x)
#endif
#endif

struct slabentry {
    uint32_t destaddr;
    uint32_t length_flag;
};

struct slabheader {
    uint16_t entries;
    uint16_t headersize;
    struct slabentry blocks[0];
};

struct nameentry {
    uint8_t segtype;
    uint16_t dtor_offset;
    char name[0];
};

int slabextract(const unsigned char *buffer, int bufferlen)
{
    const struct slabheader *h = (const void *) buffer;
    const unsigned char *listpointer;
    const unsigned char *datapointer;
    int i, count, headersize;

    headersize = le16toh(h->headersize);
    count = le16toh(h->entries);
    if ((headersize < ((count * 8) + 4)) || (bufferlen < headersize)) {
	fprintf(stderr, "Invalid file header - probably not a SLAB file\n");
	return 1;
    }
    printf("%d entries\n", count);

    /* FIXME: Is the 37 really constant? */
    if (((8 * count) + 37) < headersize) {
        listpointer = buffer + 8 * count + 37;
        printf("Name            Tp ");
    } else {
        listpointer = NULL;	/* No names present */
        printf("Name    ");
    }

    datapointer = buffer + le32toh(h->headersize);

    printf("LoadAddr     size initialized\n");

    for (i = 0; i < count; i++) {
        const struct slabentry *block;
	char filename[25];
	uint32_t len;
        int has_data;

        if (listpointer) {
            const struct nameentry *entry = (const void *) listpointer;
            block = (const void *) (buffer + le16toh(entry->dtor_offset));
            sprintf(filename, "%.20s.bin", entry->name);
            listpointer += strlen(entry->name) + 4;
            printf("%-15s %02x ", entry->name, entry->segtype);
        } else {
            block = (const void *) (buffer + 4 + 8 * i);
            sprintf(filename, "block%02d.bin", i);
            printf("block%02d ", i);
        }

	len = le32toh(block->length_flag);
	if (len & 0x80000000)
	    has_data = 1;
	else
	    has_data = 0;
        len &= 0x7fffffff;

        printf("%08x %8d\t %s\n", le32toh(block->destaddr), len,
               has_data ? "yes" : "no");

        if (has_data) {
	    int outfd;

	    if (datapointer + len > buffer + bufferlen) {
		fprintf(stderr, "Not enough data. File truncated?\n");
		return 1;
	    }
	    outfd = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
			 S_IRUSR | S_IWUSR);
            if (outfd != -1) {
		int ret = write(outfd, datapointer, len);
		if (ret == -1)
		    fprintf(stderr, "Can't write %s: %s\n", filename,
			    strerror(errno));
		else if (ret < len)
		    fprintf(stderr, "Can't write %s completely: Disk full?\n",
			    filename);
		close(outfd);
            } else
                fprintf(stderr, "Can't create output file %s: %s\n", filename,
			strerror(errno));
            datapointer += len;
        }
    }

    if (datapointer != buffer + bufferlen)
	fprintf(stderr, "Warning: Unexpected %d trailing bytes",
		(int)(buffer + bufferlen - datapointer));

    return 0;
}

int main(int argc, char *argv[])
{
    int infd;
    unsigned char *InputBuffer;
    int InputBufferSize;

    if (argc != 2) {
	printf("usage: %s <input file>\n", argv[0]);
	return 1;
    }

    infd = open(argv[1], O_RDONLY);
    if (infd < 0) {
	fprintf(stderr, "Error: Failed to open %s: %s\n",
		argv[1], strerror(errno));
	return 1;
    }

    InputBufferSize = lseek(infd, 0, SEEK_END);
    if (InputBufferSize < 0) {
	fprintf(stderr, "Error: Failed to lseek \"%s\": %s\n",
		argv[1], strerror(errno));
	return 1;
    }

    InputBuffer = mmap(NULL, InputBufferSize, PROT_READ, MAP_PRIVATE, infd, 0);
    if (InputBuffer < 0) {
	fprintf(stderr, "Error: Failed to mmap %s: %s\n",
		argv[1], strerror(errno));
	return 1;
    }

    /* fixed header size - everything else is checked dynamically in
       slabextract */
    if (InputBufferSize < 4) {
	fprintf(stderr, "Error: \"%s\" is too small to be a SLAB file.\n",
		argv[1]);
	return 1;
    }

    return slabextract(InputBuffer, InputBufferSize);
}