diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /arch/alpha/boot/tools |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'arch/alpha/boot/tools')
-rw-r--r-- | arch/alpha/boot/tools/mkbb.c | 151 | ||||
-rw-r--r-- | arch/alpha/boot/tools/objstrip.c | 281 |
2 files changed, 432 insertions, 0 deletions
diff --git a/arch/alpha/boot/tools/mkbb.c b/arch/alpha/boot/tools/mkbb.c new file mode 100644 index 000000000000..23c7190b047c --- /dev/null +++ b/arch/alpha/boot/tools/mkbb.c | |||
@@ -0,0 +1,151 @@ | |||
1 | /* This utility makes a bootblock suitable for the SRM console/miniloader */ | ||
2 | |||
3 | /* Usage: | ||
4 | * mkbb <device> <lxboot> | ||
5 | * | ||
6 | * Where <device> is the name of the device to install the bootblock on, | ||
7 | * and <lxboot> is the name of a bootblock to merge in. This bootblock | ||
8 | * contains the offset and size of the bootloader. It must be exactly | ||
9 | * 512 bytes long. | ||
10 | */ | ||
11 | |||
12 | #include <fcntl.h> | ||
13 | #include <unistd.h> | ||
14 | #include <stdio.h> | ||
15 | |||
16 | /* Minimal definition of disklabel, so we don't have to include | ||
17 | * asm/disklabel.h (confuses make) | ||
18 | */ | ||
19 | #ifndef MAXPARTITIONS | ||
20 | #define MAXPARTITIONS 8 /* max. # of partitions */ | ||
21 | #endif | ||
22 | |||
23 | #ifndef u8 | ||
24 | #define u8 unsigned char | ||
25 | #endif | ||
26 | |||
27 | #ifndef u16 | ||
28 | #define u16 unsigned short | ||
29 | #endif | ||
30 | |||
31 | #ifndef u32 | ||
32 | #define u32 unsigned int | ||
33 | #endif | ||
34 | |||
35 | struct disklabel { | ||
36 | u32 d_magic; /* must be DISKLABELMAGIC */ | ||
37 | u16 d_type, d_subtype; | ||
38 | u8 d_typename[16]; | ||
39 | u8 d_packname[16]; | ||
40 | u32 d_secsize; | ||
41 | u32 d_nsectors; | ||
42 | u32 d_ntracks; | ||
43 | u32 d_ncylinders; | ||
44 | u32 d_secpercyl; | ||
45 | u32 d_secprtunit; | ||
46 | u16 d_sparespertrack; | ||
47 | u16 d_sparespercyl; | ||
48 | u32 d_acylinders; | ||
49 | u16 d_rpm, d_interleave, d_trackskew, d_cylskew; | ||
50 | u32 d_headswitch, d_trkseek, d_flags; | ||
51 | u32 d_drivedata[5]; | ||
52 | u32 d_spare[5]; | ||
53 | u32 d_magic2; /* must be DISKLABELMAGIC */ | ||
54 | u16 d_checksum; | ||
55 | u16 d_npartitions; | ||
56 | u32 d_bbsize, d_sbsize; | ||
57 | struct d_partition { | ||
58 | u32 p_size; | ||
59 | u32 p_offset; | ||
60 | u32 p_fsize; | ||
61 | u8 p_fstype; | ||
62 | u8 p_frag; | ||
63 | u16 p_cpg; | ||
64 | } d_partitions[MAXPARTITIONS]; | ||
65 | }; | ||
66 | |||
67 | |||
68 | typedef union __bootblock { | ||
69 | struct { | ||
70 | char __pad1[64]; | ||
71 | struct disklabel __label; | ||
72 | } __u1; | ||
73 | struct { | ||
74 | unsigned long __pad2[63]; | ||
75 | unsigned long __checksum; | ||
76 | } __u2; | ||
77 | char bootblock_bytes[512]; | ||
78 | unsigned long bootblock_quadwords[64]; | ||
79 | } bootblock; | ||
80 | |||
81 | #define bootblock_label __u1.__label | ||
82 | #define bootblock_checksum __u2.__checksum | ||
83 | |||
84 | main(int argc, char ** argv) | ||
85 | { | ||
86 | bootblock bootblock_from_disk; | ||
87 | bootblock bootloader_image; | ||
88 | int dev, fd; | ||
89 | int i; | ||
90 | int nread; | ||
91 | |||
92 | /* Make sure of the arg count */ | ||
93 | if(argc != 3) { | ||
94 | fprintf(stderr, "Usage: %s device lxboot\n", argv[0]); | ||
95 | exit(0); | ||
96 | } | ||
97 | |||
98 | /* First, open the device and make sure it's accessible */ | ||
99 | dev = open(argv[1], O_RDWR); | ||
100 | if(dev < 0) { | ||
101 | perror(argv[1]); | ||
102 | exit(0); | ||
103 | } | ||
104 | |||
105 | /* Now open the lxboot and make sure it's reasonable */ | ||
106 | fd = open(argv[2], O_RDONLY); | ||
107 | if(fd < 0) { | ||
108 | perror(argv[2]); | ||
109 | close(dev); | ||
110 | exit(0); | ||
111 | } | ||
112 | |||
113 | /* Read in the lxboot */ | ||
114 | nread = read(fd, &bootloader_image, sizeof(bootblock)); | ||
115 | if(nread != sizeof(bootblock)) { | ||
116 | perror("lxboot read"); | ||
117 | fprintf(stderr, "expected %d, got %d\n", sizeof(bootblock), nread); | ||
118 | exit(0); | ||
119 | } | ||
120 | |||
121 | /* Read in the bootblock from disk. */ | ||
122 | nread = read(dev, &bootblock_from_disk, sizeof(bootblock)); | ||
123 | if(nread != sizeof(bootblock)) { | ||
124 | perror("bootblock read"); | ||
125 | fprintf(stderr, "expected %d, got %d\n", sizeof(bootblock), nread); | ||
126 | exit(0); | ||
127 | } | ||
128 | |||
129 | /* Swap the bootblock's disklabel into the bootloader */ | ||
130 | bootloader_image.bootblock_label = bootblock_from_disk.bootblock_label; | ||
131 | |||
132 | /* Calculate the bootblock checksum */ | ||
133 | bootloader_image.bootblock_checksum = 0; | ||
134 | for(i = 0; i < 63; i++) { | ||
135 | bootloader_image.bootblock_checksum += | ||
136 | bootloader_image.bootblock_quadwords[i]; | ||
137 | } | ||
138 | |||
139 | /* Write the whole thing out! */ | ||
140 | lseek(dev, 0L, SEEK_SET); | ||
141 | if(write(dev, &bootloader_image, sizeof(bootblock)) != sizeof(bootblock)) { | ||
142 | perror("bootblock write"); | ||
143 | exit(0); | ||
144 | } | ||
145 | |||
146 | close(fd); | ||
147 | close(dev); | ||
148 | exit(0); | ||
149 | } | ||
150 | |||
151 | |||
diff --git a/arch/alpha/boot/tools/objstrip.c b/arch/alpha/boot/tools/objstrip.c new file mode 100644 index 000000000000..67beb1b45e4f --- /dev/null +++ b/arch/alpha/boot/tools/objstrip.c | |||
@@ -0,0 +1,281 @@ | |||
1 | /* | ||
2 | * arch/alpha/boot/tools/objstrip.c | ||
3 | * | ||
4 | * Strip the object file headers/trailers from an executable (ELF or ECOFF). | ||
5 | * | ||
6 | * Copyright (C) 1996 David Mosberger-Tang. | ||
7 | */ | ||
8 | /* | ||
9 | * Converts an ECOFF or ELF object file into a bootable file. The | ||
10 | * object file must be a OMAGIC file (i.e., data and bss follow immediately | ||
11 | * behind the text). See DEC "Assembly Language Programmer's Guide" | ||
12 | * documentation for details. The SRM boot process is documented in | ||
13 | * the Alpha AXP Architecture Reference Manual, Second Edition by | ||
14 | * Richard L. Sites and Richard T. Witek. | ||
15 | */ | ||
16 | #include <stdio.h> | ||
17 | #include <string.h> | ||
18 | #include <stdlib.h> | ||
19 | #include <unistd.h> | ||
20 | |||
21 | #include <sys/fcntl.h> | ||
22 | #include <sys/stat.h> | ||
23 | #include <sys/types.h> | ||
24 | |||
25 | #include <linux/a.out.h> | ||
26 | #include <linux/coff.h> | ||
27 | #include <linux/param.h> | ||
28 | #include <linux/string.h> | ||
29 | #ifdef __ELF__ | ||
30 | # include <linux/elf.h> | ||
31 | #endif | ||
32 | |||
33 | /* bootfile size must be multiple of BLOCK_SIZE: */ | ||
34 | #define BLOCK_SIZE 512 | ||
35 | |||
36 | const char * prog_name; | ||
37 | |||
38 | |||
39 | void | ||
40 | usage (void) | ||
41 | { | ||
42 | fprintf(stderr, | ||
43 | "usage: %s [-v] -p file primary\n" | ||
44 | " %s [-vb] file [secondary]\n", prog_name, prog_name); | ||
45 | exit(1); | ||
46 | } | ||
47 | |||
48 | |||
49 | int | ||
50 | main (int argc, char *argv[]) | ||
51 | { | ||
52 | size_t nwritten, tocopy, n, mem_size, fil_size, pad = 0; | ||
53 | int fd, ofd, i, j, verbose = 0, primary = 0; | ||
54 | char buf[8192], *inname; | ||
55 | struct exec * aout; /* includes file & aout header */ | ||
56 | long offset; | ||
57 | #ifdef __ELF__ | ||
58 | struct elfhdr *elf; | ||
59 | struct elf_phdr *elf_phdr; /* program header */ | ||
60 | unsigned long long e_entry; | ||
61 | #endif | ||
62 | |||
63 | prog_name = argv[0]; | ||
64 | |||
65 | for (i = 1; i < argc && argv[i][0] == '-'; ++i) { | ||
66 | for (j = 1; argv[i][j]; ++j) { | ||
67 | switch (argv[i][j]) { | ||
68 | case 'v': | ||
69 | verbose = ~verbose; | ||
70 | break; | ||
71 | |||
72 | case 'b': | ||
73 | pad = BLOCK_SIZE; | ||
74 | break; | ||
75 | |||
76 | case 'p': | ||
77 | primary = 1; /* make primary bootblock */ | ||
78 | break; | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | if (i >= argc) { | ||
84 | usage(); | ||
85 | } | ||
86 | inname = argv[i++]; | ||
87 | |||
88 | fd = open(inname, O_RDONLY); | ||
89 | if (fd == -1) { | ||
90 | perror("open"); | ||
91 | exit(1); | ||
92 | } | ||
93 | |||
94 | ofd = 1; | ||
95 | if (i < argc) { | ||
96 | ofd = open(argv[i++], O_WRONLY | O_CREAT | O_TRUNC, 0666); | ||
97 | if (fd == -1) { | ||
98 | perror("open"); | ||
99 | exit(1); | ||
100 | } | ||
101 | } | ||
102 | |||
103 | if (primary) { | ||
104 | /* generate bootblock for primary loader */ | ||
105 | |||
106 | unsigned long bb[64], sum = 0; | ||
107 | struct stat st; | ||
108 | off_t size; | ||
109 | int i; | ||
110 | |||
111 | if (ofd == 1) { | ||
112 | usage(); | ||
113 | } | ||
114 | |||
115 | if (fstat(fd, &st) == -1) { | ||
116 | perror("fstat"); | ||
117 | exit(1); | ||
118 | } | ||
119 | |||
120 | size = (st.st_size + BLOCK_SIZE - 1) & ~(BLOCK_SIZE - 1); | ||
121 | memset(bb, 0, sizeof(bb)); | ||
122 | strcpy((char *) bb, "Linux SRM bootblock"); | ||
123 | bb[60] = size / BLOCK_SIZE; /* count */ | ||
124 | bb[61] = 1; /* starting sector # */ | ||
125 | bb[62] = 0; /* flags---must be 0 */ | ||
126 | for (i = 0; i < 63; ++i) { | ||
127 | sum += bb[i]; | ||
128 | } | ||
129 | bb[63] = sum; | ||
130 | if (write(ofd, bb, sizeof(bb)) != sizeof(bb)) { | ||
131 | perror("boot-block write"); | ||
132 | exit(1); | ||
133 | } | ||
134 | printf("%lu\n", size); | ||
135 | return 0; | ||
136 | } | ||
137 | |||
138 | /* read and inspect exec header: */ | ||
139 | |||
140 | if (read(fd, buf, sizeof(buf)) < 0) { | ||
141 | perror("read"); | ||
142 | exit(1); | ||
143 | } | ||
144 | |||
145 | #ifdef __ELF__ | ||
146 | elf = (struct elfhdr *) buf; | ||
147 | |||
148 | if (elf->e_ident[0] == 0x7f && strncmp(elf->e_ident + 1, "ELF", 3) == 0) { | ||
149 | if (elf->e_type != ET_EXEC) { | ||
150 | fprintf(stderr, "%s: %s is not an ELF executable\n", | ||
151 | prog_name, inname); | ||
152 | exit(1); | ||
153 | } | ||
154 | if (!elf_check_arch(elf)) { | ||
155 | fprintf(stderr, "%s: is not for this processor (e_machine=%d)\n", | ||
156 | prog_name, elf->e_machine); | ||
157 | exit(1); | ||
158 | } | ||
159 | if (elf->e_phnum != 1) { | ||
160 | fprintf(stderr, | ||
161 | "%s: %d program headers (forgot to link with -N?)\n", | ||
162 | prog_name, elf->e_phnum); | ||
163 | } | ||
164 | |||
165 | e_entry = elf->e_entry; | ||
166 | |||
167 | lseek(fd, elf->e_phoff, SEEK_SET); | ||
168 | if (read(fd, buf, sizeof(*elf_phdr)) != sizeof(*elf_phdr)) { | ||
169 | perror("read"); | ||
170 | exit(1); | ||
171 | } | ||
172 | |||
173 | elf_phdr = (struct elf_phdr *) buf; | ||
174 | offset = elf_phdr->p_offset; | ||
175 | mem_size = elf_phdr->p_memsz; | ||
176 | fil_size = elf_phdr->p_filesz; | ||
177 | |||
178 | /* work around ELF bug: */ | ||
179 | if (elf_phdr->p_vaddr < e_entry) { | ||
180 | unsigned long delta = e_entry - elf_phdr->p_vaddr; | ||
181 | offset += delta; | ||
182 | mem_size -= delta; | ||
183 | fil_size -= delta; | ||
184 | elf_phdr->p_vaddr += delta; | ||
185 | } | ||
186 | |||
187 | if (verbose) { | ||
188 | fprintf(stderr, "%s: extracting %#016lx-%#016lx (at %lx)\n", | ||
189 | prog_name, (long) elf_phdr->p_vaddr, | ||
190 | elf_phdr->p_vaddr + fil_size, offset); | ||
191 | } | ||
192 | } else | ||
193 | #endif | ||
194 | { | ||
195 | aout = (struct exec *) buf; | ||
196 | |||
197 | if (!(aout->fh.f_flags & COFF_F_EXEC)) { | ||
198 | fprintf(stderr, "%s: %s is not in executable format\n", | ||
199 | prog_name, inname); | ||
200 | exit(1); | ||
201 | } | ||
202 | |||
203 | if (aout->fh.f_opthdr != sizeof(aout->ah)) { | ||
204 | fprintf(stderr, "%s: %s has unexpected optional header size\n", | ||
205 | prog_name, inname); | ||
206 | exit(1); | ||
207 | } | ||
208 | |||
209 | if (N_MAGIC(*aout) != OMAGIC) { | ||
210 | fprintf(stderr, "%s: %s is not an OMAGIC file\n", | ||
211 | prog_name, inname); | ||
212 | exit(1); | ||
213 | } | ||
214 | offset = N_TXTOFF(*aout); | ||
215 | fil_size = aout->ah.tsize + aout->ah.dsize; | ||
216 | mem_size = fil_size + aout->ah.bsize; | ||
217 | |||
218 | if (verbose) { | ||
219 | fprintf(stderr, "%s: extracting %#016lx-%#016lx (at %lx)\n", | ||
220 | prog_name, aout->ah.text_start, | ||
221 | aout->ah.text_start + fil_size, offset); | ||
222 | } | ||
223 | } | ||
224 | |||
225 | if (lseek(fd, offset, SEEK_SET) != offset) { | ||
226 | perror("lseek"); | ||
227 | exit(1); | ||
228 | } | ||
229 | |||
230 | if (verbose) { | ||
231 | fprintf(stderr, "%s: copying %lu byte from %s\n", | ||
232 | prog_name, (unsigned long) fil_size, inname); | ||
233 | } | ||
234 | |||
235 | tocopy = fil_size; | ||
236 | while (tocopy > 0) { | ||
237 | n = tocopy; | ||
238 | if (n > sizeof(buf)) { | ||
239 | n = sizeof(buf); | ||
240 | } | ||
241 | tocopy -= n; | ||
242 | if ((size_t) read(fd, buf, n) != n) { | ||
243 | perror("read"); | ||
244 | exit(1); | ||
245 | } | ||
246 | do { | ||
247 | nwritten = write(ofd, buf, n); | ||
248 | if ((ssize_t) nwritten == -1) { | ||
249 | perror("write"); | ||
250 | exit(1); | ||
251 | } | ||
252 | n -= nwritten; | ||
253 | } while (n > 0); | ||
254 | } | ||
255 | |||
256 | if (pad) { | ||
257 | mem_size = ((mem_size + pad - 1) / pad) * pad; | ||
258 | } | ||
259 | |||
260 | tocopy = mem_size - fil_size; | ||
261 | if (tocopy > 0) { | ||
262 | fprintf(stderr, | ||
263 | "%s: zero-filling bss and aligning to %lu with %lu bytes\n", | ||
264 | prog_name, pad, (unsigned long) tocopy); | ||
265 | |||
266 | memset(buf, 0x00, sizeof(buf)); | ||
267 | do { | ||
268 | n = tocopy; | ||
269 | if (n > sizeof(buf)) { | ||
270 | n = sizeof(buf); | ||
271 | } | ||
272 | nwritten = write(ofd, buf, n); | ||
273 | if ((ssize_t) nwritten == -1) { | ||
274 | perror("write"); | ||
275 | exit(1); | ||
276 | } | ||
277 | tocopy -= nwritten; | ||
278 | } while (tocopy > 0); | ||
279 | } | ||
280 | return 0; | ||
281 | } | ||