Skip to content

Commit 66a45dd

Browse files
committed
powerpc: Make COFF zImages for old 32-bit powermacs
This adds code to build zImage.coff and/or zImage.initrd.coff when CONFIG_PPC32 and CONFIG_PPC_PMAC are defined. It also restructures the OF client code and adds some workarounds for OF quirks on the older machines. Signed-off-by: Paul Mackerras <paulus@samba.org>
1 parent 3687457 commit 66a45dd

10 files changed

Lines changed: 895 additions & 499 deletions

File tree

‎arch/powerpc/boot/Makefile‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ HOSTCC := gcc
2525
BOOTCFLAGS := $(HOSTCFLAGS) -fno-builtin -nostdinc -isystem \
2626
$(shell $(CROSS32CC) -print-file-name=include) -fPIC
2727
BOOTAFLAGS := -D__ASSEMBLY__ $(BOOTCFLAGS) -traditional -nostdinc
28-
BOOTLFLAGS := -T $(srctree)/$(src)/zImage.lds
2928
OBJCOPYFLAGS := contents,alloc,load,readonly,data
29+
OBJCOPY_COFF_ARGS := -O aixcoff-rs6000 --set-start 0x500000
3030

3131
zlib := infblock.c infcodes.c inffast.c inflate.c inftrees.c infutil.c
3232
zlibheader := infblock.h infcodes.h inffast.h inftrees.h infutil.h
@@ -35,7 +35,7 @@ zliblinuxheader := zlib.h zconf.h zutil.h
3535
$(addprefix $(obj)/,$(zlib) main.o): $(addprefix $(obj)/,$(zliblinuxheader)) $(addprefix $(obj)/,$(zlibheader))
3636
#$(addprefix $(obj)/,main.o): $(addprefix $(obj)/,zlib.h)
3737

38-
src-boot := string.S prom.c main.c div64.S crt0.S
38+
src-boot := crt0.S string.S prom.c stdio.c main.c div64.S
3939
src-boot += $(zlib)
4040
src-boot := $(addprefix $(obj)/, $(src-boot))
4141
obj-boot := $(addsuffix .o, $(basename $(src-boot)))
@@ -70,7 +70,7 @@ quiet_cmd_bootas = BOOTAS $@
7070
cmd_bootas = $(CROSS32CC) -Wp,-MD,$(depfile) $(BOOTAFLAGS) -c -o $@ $<
7171

7272
quiet_cmd_bootld = BOOTLD $@
73-
cmd_bootld = $(CROSS32LD) $(BOOTLFLAGS) -o $@ $(2)
73+
cmd_bootld = $(CROSS32LD) -T $(srctree)/$(src)/$(3) -o $@ $(2)
7474

7575
$(patsubst %.c,%.o, $(filter %.c, $(src-boot))): %.o: %.c
7676
$(call if_changed_dep,bootcc)
@@ -87,12 +87,14 @@ obj-sec = $(foreach section, $(1), $(patsubst %,$(obj)/kernel-%.o, $(section)))
8787
src-sec = $(foreach section, $(1), $(patsubst %,$(obj)/kernel-%.c, $(section)))
8888
gz-sec = $(foreach section, $(1), $(patsubst %,$(obj)/kernel-%.gz, $(section)))
8989

90-
hostprogs-y := addnote addRamDisk
91-
targets += zImage.vmode zImage.initrd.vmode zImage zImage.initrd \
92-
$(patsubst $(obj)/%,%, $(call obj-sec, $(required) $(initrd))) \
93-
$(patsubst $(obj)/%,%, $(call src-sec, $(required) $(initrd))) \
94-
$(patsubst $(obj)/%,%, $(call gz-sec, $(required) $(initrd))) \
95-
vmlinux.initrd
90+
hostprogs-y := addnote addRamDisk hack-coff
91+
92+
targets += zImage.vmode zImage.initrd.vmode zImage zImage.initrd \
93+
zImage.coff zImage.initrd.coff \
94+
$(patsubst $(obj)/%,%, $(call obj-sec, $(required) $(initrd))) \
95+
$(patsubst $(obj)/%,%, $(call src-sec, $(required) $(initrd))) \
96+
$(patsubst $(obj)/%,%, $(call gz-sec, $(required) $(initrd))) \
97+
vmlinux.initrd
9698
extra-y := initrd.o
9799

98100
quiet_cmd_ramdisk = RAMDISK $@
@@ -114,6 +116,10 @@ quiet_cmd_addsection = ADDSEC $@
114116
quiet_cmd_addnote = ADDNOTE $@
115117
cmd_addnote = $(obj)/addnote $@
116118

119+
quiet_cmd_gencoff = COFF $@
120+
cmd_gencoff = $(OBJCOPY) $(OBJCOPY_COFF_ARGS) $@ && \
121+
$(obj)/hack-coff $@
122+
117123
$(call gz-sec, $(required)): $(obj)/kernel-%.gz: %
118124
$(call if_changed,gzip)
119125

@@ -127,22 +133,35 @@ $(call obj-sec, $(required) $(initrd)): $(obj)/kernel-%.o: $(obj)/kernel-%.c
127133
$(call if_changed_dep,bootcc)
128134
$(call cmd,addsection)
129135

130-
$(obj)/zImage.vmode: obj-boot += $(call obj-sec, $(required))
136+
$(obj)/zImage.vmode $(obj)/zImage.coff: obj-boot += $(call obj-sec, $(required))
131137
$(obj)/zImage.vmode: $(call obj-sec, $(required)) $(obj-boot) $(srctree)/$(src)/zImage.lds
132-
$(call cmd,bootld,$(obj-boot))
138+
$(call cmd,bootld,$(obj-boot),zImage.lds)
133139

134-
$(obj)/zImage.initrd.vmode: obj-boot += $(call obj-sec, $(required) $(initrd))
140+
$(obj)/zImage.initrd.vmode $(obj)/zImage.initrd.coff: obj-boot += $(call obj-sec, $(required) $(initrd))
135141
$(obj)/zImage.initrd.vmode: $(call obj-sec, $(required) $(initrd)) $(obj-boot) $(srctree)/$(src)/zImage.lds
136-
$(call cmd,bootld,$(obj-boot))
142+
$(call cmd,bootld,$(obj-boot),zImage.lds)
143+
144+
# For 32-bit powermacs, build the COFF images as well as the ELF images.
145+
coffimage-$(CONFIG_PPC_PMAC)-$(CONFIG_PPC32) := $(obj)/zImage.coff
146+
coffrdimg-$(CONFIG_PPC_PMAC)-$(CONFIG_PPC32) := $(obj)/zImage.initrd.coff
137147

138-
$(obj)/zImage: $(obj)/zImage.vmode $(obj)/addnote
148+
$(obj)/zImage: $(obj)/zImage.vmode $(obj)/addnote $(coffimage-y-y)
139149
@cp -f $< $@
140150
$(call if_changed,addnote)
141151

142-
$(obj)/zImage.initrd: $(obj)/zImage.initrd.vmode $(obj)/addnote
152+
$(obj)/zImage.initrd: $(obj)/zImage.initrd.vmode $(obj)/addnote $(coffrdimg-y-y)
143153
@cp -f $< $@
144154
$(call if_changed,addnote)
145155

156+
$(obj)/zImage.coff: $(call obj-sec, $(required)) $(obj-boot) $(srctree)/$(src)/zImage.coff.lds $(obj)/hack-coff
157+
$(call cmd,bootld,$(obj-boot),zImage.coff.lds)
158+
$(call cmd,gencoff)
159+
160+
$(obj)/zImage.initrd.coff: $(call obj-sec, $(required) $(initrd)) $(obj-boot) \
161+
$(srctree)/$(src)/zImage.coff.lds $(obj)/hack-coff
162+
$(call cmd,bootld,$(obj-boot),zImage.coff.lds)
163+
$(call cmd,gencoff)
164+
146165
#-----------------------------------------------------------
147166
# build u-boot images
148167
#-----------------------------------------------------------

‎arch/powerpc/boot/crt0.S‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,23 @@
1212
#include "ppc_asm.h"
1313

1414
.text
15+
/* a procedure descriptor used when booting this as a COFF file */
16+
_zimage_start_opd:
17+
.long _zimage_start, 0, 0, 0
18+
1519
.globl _zimage_start
1620
_zimage_start:
21+
/* Work out the offset between the address we were linked at
22+
and the address where we're running. */
1723
bl 1f
18-
19-
1:
20-
mflr r0
24+
1: mflr r0
2125
lis r9,1b@ha
2226
addi r9,r9,1b@l
2327
subf. r0,r9,r0
24-
beq 3f
28+
beq 3f /* if running at same address as linked */
2529

30+
/* The .got2 section contains a list of addresses, so add
31+
the address offset onto each entry. */
2632
lis r9,__got2_start@ha
2733
addi r9,r9,__got2_start@l
2834
lis r8,__got2_end@ha
@@ -32,15 +38,14 @@ _zimage_start:
3238
srwi. r8,r8,2
3339
mtctr r8
3440
add r9,r0,r9
35-
2:
36-
lwz r8,0(r9)
41+
2: lwz r8,0(r9)
3742
add r8,r8,r0
3843
stw r8,0(r9)
3944
addi r9,r9,4
4045
bdnz 2b
4146

42-
3:
43-
lis r9,_start@h
47+
/* Do a cache flush for our text, in case OF didn't */
48+
3: lis r9,_start@h
4449
add r9,r0,r9
4550
lis r8,_etext@ha
4651
addi r8,r8,_etext@l

‎arch/powerpc/boot/hack-coff.c‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* hack-coff.c - hack the header of an xcoff file to fill in
3+
* a few fields needed by the Open Firmware xcoff loader on
4+
* Power Macs but not initialized by objcopy.
5+
*
6+
* Copyright (C) Paul Mackerras 1997.
7+
*
8+
* This program is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU General Public License
10+
* as published by the Free Software Foundation; either version
11+
* 2 of the License, or (at your option) any later version.
12+
*/
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
#include <unistd.h>
16+
#include <fcntl.h>
17+
#include <string.h>
18+
#include "rs6000.h"
19+
20+
#define AOUT_MAGIC 0x010b
21+
22+
#define get_16be(x) ((((unsigned char *)(x))[0] << 8) \
23+
+ ((unsigned char *)(x))[1])
24+
#define put_16be(x, v) (((unsigned char *)(x))[0] = (v) >> 8, \
25+
((unsigned char *)(x))[1] = (v) & 0xff)
26+
#define get_32be(x) ((((unsigned char *)(x))[0] << 24) \
27+
+ (((unsigned char *)(x))[1] << 16) \
28+
+ (((unsigned char *)(x))[2] << 8) \
29+
+ ((unsigned char *)(x))[3])
30+
31+
int
32+
main(int ac, char **av)
33+
{
34+
int fd;
35+
int i, nsect;
36+
int aoutsz;
37+
struct external_filehdr fhdr;
38+
AOUTHDR aout;
39+
struct external_scnhdr shdr;
40+
41+
if (ac != 2) {
42+
fprintf(stderr, "Usage: hack-coff coff-file\n");
43+
exit(1);
44+
}
45+
if ((fd = open(av[1], 2)) == -1) {
46+
perror(av[2]);
47+
exit(1);
48+
}
49+
if (read(fd, &fhdr, sizeof(fhdr)) != sizeof(fhdr))
50+
goto readerr;
51+
i = get_16be(fhdr.f_magic);
52+
if (i != U802TOCMAGIC && i != U802WRMAGIC && i != U802ROMAGIC) {
53+
fprintf(stderr, "%s: not an xcoff file\n", av[1]);
54+
exit(1);
55+
}
56+
aoutsz = get_16be(fhdr.f_opthdr);
57+
if (read(fd, &aout, aoutsz) != aoutsz)
58+
goto readerr;
59+
nsect = get_16be(fhdr.f_nscns);
60+
for (i = 0; i < nsect; ++i) {
61+
if (read(fd, &shdr, sizeof(shdr)) != sizeof(shdr))
62+
goto readerr;
63+
if (strcmp(shdr.s_name, ".text") == 0) {
64+
put_16be(aout.o_snentry, i+1);
65+
put_16be(aout.o_sntext, i+1);
66+
} else if (strcmp(shdr.s_name, ".data") == 0) {
67+
put_16be(aout.o_sndata, i+1);
68+
} else if (strcmp(shdr.s_name, ".bss") == 0) {
69+
put_16be(aout.o_snbss, i+1);
70+
}
71+
}
72+
put_16be(aout.magic, AOUT_MAGIC);
73+
if (lseek(fd, (long) sizeof(struct external_filehdr), 0) == -1
74+
|| write(fd, &aout, aoutsz) != aoutsz) {
75+
fprintf(stderr, "%s: write error\n", av[1]);
76+
exit(1);
77+
}
78+
close(fd);
79+
exit(0);
80+
81+
readerr:
82+
fprintf(stderr, "%s: read error or file too short\n", av[1]);
83+
exit(1);
84+
}

‎arch/powerpc/boot/main.c‎

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ extern void flush_cache(void *, unsigned long);
2121

2222

2323
/* Value picked to match that used by yaboot */
24-
#define PROG_START 0x01400000
25-
#define RAM_END (512<<20) // Fixme: use OF */
24+
#define PROG_START 0x01400000 /* only used on 64-bit systems */
25+
#define RAM_END (512<<20) /* Fixme: use OF */
2626
#define ONE_MB 0x100000
2727

2828
extern char _start[];
@@ -160,6 +160,17 @@ static int is_elf64(void *hdr)
160160
elfoffset = (unsigned long)elf64ph->p_offset;
161161
vmlinux.size = (unsigned long)elf64ph->p_filesz + elfoffset;
162162
vmlinux.memsize = (unsigned long)elf64ph->p_memsz + elfoffset;
163+
164+
#if defined(PROG_START)
165+
/*
166+
* Maintain a "magic" minimum address. This keeps some older
167+
* firmware platforms running.
168+
*/
169+
170+
if (claim_base < PROG_START)
171+
claim_base = PROG_START;
172+
#endif
173+
163174
return 1;
164175
}
165176

@@ -206,12 +217,18 @@ void start(unsigned long a1, unsigned long a2, void *promptr, void *sp)
206217
exit();
207218
if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4)
208219
exit();
209-
stderr = stdout;
210-
if (getprop(chosen_handle, "stdin", &stdin, sizeof(stdin)) != 4)
211-
exit();
212220

213221
printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r", _start, sp);
214222

223+
/*
224+
* The first available claim_base must be above the end of the
225+
* the loaded kernel wrapper file (_start to _end includes the
226+
* initrd image if it is present) and rounded up to a nice
227+
* 1 MB boundary for good measure.
228+
*/
229+
230+
claim_base = _ALIGN_UP((unsigned long)_end, ONE_MB);
231+
215232
vmlinuz.addr = (unsigned long)_vmlinux_start;
216233
vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
217234

@@ -228,25 +245,6 @@ void start(unsigned long a1, unsigned long a2, void *promptr, void *sp)
228245
exit();
229246
}
230247

231-
/*
232-
* The first available claim_base must be above the end of the
233-
* the loaded kernel wrapper file (_start to _end includes the
234-
* initrd image if it is present) and rounded up to a nice
235-
* 1 MB boundary for good measure.
236-
*/
237-
238-
claim_base = _ALIGN_UP((unsigned long)_end, ONE_MB);
239-
240-
#if defined(PROG_START)
241-
/*
242-
* Maintain a "magic" minimum address. This keeps some older
243-
* firmware platforms running.
244-
*/
245-
246-
if (claim_base < PROG_START)
247-
claim_base = PROG_START;
248-
#endif
249-
250248
/* We need to claim the memsize plus the file offset since gzip
251249
* will expand the header (file offset), then the kernel, then
252250
* possible rubbish we don't care about. But the kernel bss must

0 commit comments

Comments
 (0)