2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) Paul Mackerras 1997.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#include <stdarg.h>
|
2005-08-08 11:24:38 +08:00
|
|
|
#include <stddef.h>
|
2006-09-19 12:05:08 +08:00
|
|
|
#include "types.h"
|
|
|
|
#include "elf.h"
|
2005-08-08 11:24:38 +08:00
|
|
|
#include "string.h"
|
|
|
|
#include "stdio.h"
|
2006-09-19 12:05:08 +08:00
|
|
|
#include "page.h"
|
|
|
|
#include "ops.h"
|
2005-06-08 13:12:00 +08:00
|
|
|
|
2006-09-19 12:05:08 +08:00
|
|
|
typedef void *ihandle;
|
|
|
|
typedef void *phandle;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-09-19 12:05:08 +08:00
|
|
|
extern char _end[];
|
|
|
|
|
|
|
|
/* Value picked to match that used by yaboot */
|
|
|
|
#define PROG_START 0x01400000 /* only used on 64-bit systems */
|
|
|
|
#define RAM_END (512<<20) /* Fixme: use OF */
|
|
|
|
#define ONE_MB 0x100000
|
|
|
|
|
|
|
|
int (*prom) (void *);
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned long claim_base;
|
|
|
|
|
|
|
|
static int call_prom(const char *service, int nargs, int nret, ...)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-01-14 12:04:06 +08:00
|
|
|
int i;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct prom_args {
|
2006-01-14 12:04:06 +08:00
|
|
|
const char *service;
|
2005-04-17 06:20:36 +08:00
|
|
|
int nargs;
|
|
|
|
int nret;
|
2006-01-14 12:04:06 +08:00
|
|
|
unsigned int args[12];
|
2005-04-17 06:20:36 +08:00
|
|
|
} args;
|
2006-01-14 12:04:06 +08:00
|
|
|
va_list list;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
args.service = service;
|
|
|
|
args.nargs = nargs;
|
|
|
|
args.nret = nret;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
va_start(list, nret);
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
args.args[i] = va_arg(list, unsigned int);
|
|
|
|
va_end(list);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
for (i = 0; i < nret; i++)
|
|
|
|
args.args[nargs+i] = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
if (prom(&args) < 0)
|
|
|
|
return -1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
return (nret > 0)? args.args[nargs]: 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-09-19 12:05:08 +08:00
|
|
|
static int call_prom_ret(const char *service, int nargs, int nret,
|
2006-01-14 12:04:06 +08:00
|
|
|
unsigned int *rets, ...)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-01-14 12:04:06 +08:00
|
|
|
int i;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct prom_args {
|
2006-01-14 12:04:06 +08:00
|
|
|
const char *service;
|
2005-04-17 06:20:36 +08:00
|
|
|
int nargs;
|
|
|
|
int nret;
|
2006-01-14 12:04:06 +08:00
|
|
|
unsigned int args[12];
|
2005-04-17 06:20:36 +08:00
|
|
|
} args;
|
2006-01-14 12:04:06 +08:00
|
|
|
va_list list;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
args.service = service;
|
|
|
|
args.nargs = nargs;
|
|
|
|
args.nret = nret;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
va_start(list, rets);
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
args.args[i] = va_arg(list, unsigned int);
|
|
|
|
va_end(list);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
for (i = 0; i < nret; i++)
|
|
|
|
args.args[nargs+i] = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
if (prom(&args) < 0)
|
|
|
|
return -1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
if (rets != (void *) 0)
|
|
|
|
for (i = 1; i < nret; ++i)
|
|
|
|
rets[i-1] = args.args[nargs+i];
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
return (nret > 0)? args.args[nargs]: 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
/*
|
|
|
|
* Older OF's require that when claiming a specific range of addresses,
|
|
|
|
* we claim the physical space in the /memory node and the virtual
|
|
|
|
* space in the chosen mmu node, and then do a map operation to
|
|
|
|
* map virtual to physical.
|
2005-08-08 11:24:38 +08:00
|
|
|
*/
|
2006-01-14 12:04:06 +08:00
|
|
|
static int need_map = -1;
|
|
|
|
static ihandle chosen_mmu;
|
|
|
|
static phandle memory;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
/* returns true if s2 is a prefix of s1 */
|
|
|
|
static int string_match(const char *s1, const char *s2)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-01-14 12:04:06 +08:00
|
|
|
for (; *s2; ++s2)
|
|
|
|
if (*s1++ != *s2)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
static int check_of_version(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-01-14 12:04:06 +08:00
|
|
|
phandle oprom, chosen;
|
|
|
|
char version[64];
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
oprom = finddevice("/openprom");
|
|
|
|
if (oprom == (phandle) -1)
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
2006-01-14 12:04:06 +08:00
|
|
|
if (getprop(oprom, "model", version, sizeof(version)) <= 0)
|
|
|
|
return 0;
|
|
|
|
version[sizeof(version)-1] = 0;
|
|
|
|
printf("OF version = '%s'\r\n", version);
|
|
|
|
if (!string_match(version, "Open Firmware, 1.")
|
|
|
|
&& !string_match(version, "FirmWorks,3."))
|
|
|
|
return 0;
|
|
|
|
chosen = finddevice("/chosen");
|
|
|
|
if (chosen == (phandle) -1) {
|
|
|
|
chosen = finddevice("/chosen@0");
|
|
|
|
if (chosen == (phandle) -1) {
|
|
|
|
printf("no chosen\n");
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
2006-01-14 12:04:06 +08:00
|
|
|
if (getprop(chosen, "mmu", &chosen_mmu, sizeof(chosen_mmu)) <= 0) {
|
|
|
|
printf("no mmu\n");
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2006-01-14 12:04:06 +08:00
|
|
|
memory = (ihandle) call_prom("open", 1, 1, "/memory");
|
|
|
|
if (memory == (ihandle) -1) {
|
|
|
|
memory = (ihandle) call_prom("open", 1, 1, "/memory@0");
|
|
|
|
if (memory == (ihandle) -1) {
|
|
|
|
printf("no memory node\n");
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
2006-01-14 12:04:06 +08:00
|
|
|
printf("old OF detected\r\n");
|
|
|
|
return 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-09-19 12:05:08 +08:00
|
|
|
static void *claim(unsigned long virt, unsigned long size, unsigned long align)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-01-14 12:04:06 +08:00
|
|
|
int ret;
|
|
|
|
unsigned int result;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
if (need_map < 0)
|
|
|
|
need_map = check_of_version();
|
|
|
|
if (align || !need_map)
|
|
|
|
return (void *) call_prom("claim", 3, 1, virt, size, align);
|
2006-09-19 12:05:08 +08:00
|
|
|
|
2006-01-14 12:04:06 +08:00
|
|
|
ret = call_prom_ret("call-method", 5, 2, &result, "claim", memory,
|
|
|
|
align, size, virt);
|
|
|
|
if (ret != 0 || result == -1)
|
|
|
|
return (void *) -1;
|
|
|
|
ret = call_prom_ret("call-method", 5, 2, &result, "claim", chosen_mmu,
|
|
|
|
align, size, virt);
|
|
|
|
/* 0x12 == coherent + read/write */
|
|
|
|
ret = call_prom("call-method", 6, 1, "map", chosen_mmu,
|
|
|
|
0x12, size, virt, virt);
|
|
|
|
return (void *) virt;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2006-09-19 12:05:08 +08:00
|
|
|
|
2007-04-18 17:24:12 +08:00
|
|
|
static void *of_try_claim(unsigned long size)
|
2006-09-19 12:05:08 +08:00
|
|
|
{
|
|
|
|
unsigned long addr = 0;
|
|
|
|
|
2006-10-05 12:18:46 +08:00
|
|
|
if (claim_base == 0)
|
2006-09-19 12:05:08 +08:00
|
|
|
claim_base = _ALIGN_UP((unsigned long)_end, ONE_MB);
|
|
|
|
|
|
|
|
for(; claim_base < RAM_END; claim_base += ONE_MB) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf(" trying: 0x%08lx\n\r", claim_base);
|
|
|
|
#endif
|
|
|
|
addr = (unsigned long)claim(claim_base, size, 0);
|
|
|
|
if ((void *)addr != (void *)-1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (addr == 0)
|
|
|
|
return NULL;
|
|
|
|
claim_base = PAGE_ALIGN(claim_base + size);
|
|
|
|
return (void *)addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void of_image_hdr(const void *hdr)
|
|
|
|
{
|
|
|
|
const Elf64_Ehdr *elf64 = hdr;
|
|
|
|
|
|
|
|
if (elf64->e_ident[EI_CLASS] == ELFCLASS64) {
|
|
|
|
/*
|
|
|
|
* Maintain a "magic" minimum address. This keeps some older
|
|
|
|
* firmware platforms running.
|
|
|
|
*/
|
|
|
|
if (claim_base < PROG_START)
|
|
|
|
claim_base = PROG_START;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-05 11:24:52 +08:00
|
|
|
static void *of_vmlinux_alloc(unsigned long size)
|
|
|
|
{
|
|
|
|
void *p = malloc(size);
|
|
|
|
|
2007-03-21 23:02:44 +08:00
|
|
|
if (!p)
|
|
|
|
fatal("Can't allocate memory for kernel image!\n\r");
|
|
|
|
|
2007-03-05 11:24:52 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2006-09-19 12:05:08 +08:00
|
|
|
static void of_exit(void)
|
|
|
|
{
|
|
|
|
call_prom("exit", 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OF device tree routines
|
|
|
|
*/
|
|
|
|
static void *of_finddevice(const char *name)
|
|
|
|
{
|
|
|
|
return (phandle) call_prom("finddevice", 1, 1, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int of_getprop(const void *phandle, const char *name, void *buf,
|
|
|
|
const int buflen)
|
|
|
|
{
|
|
|
|
return call_prom("getprop", 4, 1, phandle, name, buf, buflen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int of_setprop(const void *phandle, const char *name, const void *buf,
|
|
|
|
const int buflen)
|
|
|
|
{
|
|
|
|
return call_prom("setprop", 4, 1, phandle, name, buf, buflen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OF console routines
|
|
|
|
*/
|
|
|
|
static void *of_stdout_handle;
|
|
|
|
|
|
|
|
static int of_console_open(void)
|
|
|
|
{
|
|
|
|
void *devp;
|
|
|
|
|
|
|
|
if (((devp = finddevice("/chosen")) != NULL)
|
|
|
|
&& (getprop(devp, "stdout", &of_stdout_handle,
|
|
|
|
sizeof(of_stdout_handle))
|
|
|
|
== sizeof(of_stdout_handle)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void of_console_write(char *buf, int len)
|
|
|
|
{
|
|
|
|
call_prom("write", 3, 1, of_stdout_handle, buf, len);
|
|
|
|
}
|
|
|
|
|
[POWERPC] zImage: Cleanup and improve zImage entry point
This patch re-organises the way the zImage wrapper code is entered, to
allow more flexibility on platforms with unusual entry conditions.
After this patch, a platform .o file has two options:
1) It can define a _zimage_start, in which case the platform code gets
control from the very beginning of execution. In this case the
platform code is responsible for relocating the zImage if necessary,
clearing the BSS, performing any platform specific initialization, and
finally calling start() to load and enter the kernel.
2) It can define platform_init(). In this case the generic crt0.S
handles initial entry, and calls platform_init() before calling
start(). The signature of platform_init() is changed, however, to
take up to 5 parameters (in r3..r7) as they come from the platform's
initial loader, instead of a fixed set of parameters based on OF's
usage.
When using the generic crt0.S, the platform .o can optionally
supply a custom stack to use, using the BSS_STACK() macro. If this
is not supplied, the crt0.S will assume that the loader has
supplied a usable stack.
In either case, the platform code communicates information to the
generic code (specifically, a PROM pointer for OF systems, and/or an
initrd image address supplied by the bootloader) via a global
structure "loader_info".
In addition the wrapper script is rearranged to ensure that the
platform .o is always linked first. This means that platforms where
the zImage entry point is at a fixed address or offset, rather than
being encoded in the binary header can be supported using option (1).
Signed-off-by: David Gibson <dwg@au1.ibm.com>
Signed-off-by: Paul Mackerras <paulus@samba.org>
2007-03-05 11:24:52 +08:00
|
|
|
void platform_init(unsigned long a1, unsigned long a2, void *promptr)
|
2006-09-19 12:05:08 +08:00
|
|
|
{
|
|
|
|
platform_ops.image_hdr = of_image_hdr;
|
|
|
|
platform_ops.malloc = of_try_claim;
|
|
|
|
platform_ops.exit = of_exit;
|
2007-03-05 11:24:52 +08:00
|
|
|
platform_ops.vmlinux_alloc = of_vmlinux_alloc;
|
2006-09-19 12:05:08 +08:00
|
|
|
|
|
|
|
dt_ops.finddevice = of_finddevice;
|
|
|
|
dt_ops.getprop = of_getprop;
|
|
|
|
dt_ops.setprop = of_setprop;
|
|
|
|
|
|
|
|
console_ops.open = of_console_open;
|
|
|
|
console_ops.write = of_console_write;
|
|
|
|
|
|
|
|
prom = (int (*)(void *))promptr;
|
[POWERPC] zImage: Cleanup and improve zImage entry point
This patch re-organises the way the zImage wrapper code is entered, to
allow more flexibility on platforms with unusual entry conditions.
After this patch, a platform .o file has two options:
1) It can define a _zimage_start, in which case the platform code gets
control from the very beginning of execution. In this case the
platform code is responsible for relocating the zImage if necessary,
clearing the BSS, performing any platform specific initialization, and
finally calling start() to load and enter the kernel.
2) It can define platform_init(). In this case the generic crt0.S
handles initial entry, and calls platform_init() before calling
start(). The signature of platform_init() is changed, however, to
take up to 5 parameters (in r3..r7) as they come from the platform's
initial loader, instead of a fixed set of parameters based on OF's
usage.
When using the generic crt0.S, the platform .o can optionally
supply a custom stack to use, using the BSS_STACK() macro. If this
is not supplied, the crt0.S will assume that the loader has
supplied a usable stack.
In either case, the platform code communicates information to the
generic code (specifically, a PROM pointer for OF systems, and/or an
initrd image address supplied by the bootloader) via a global
structure "loader_info".
In addition the wrapper script is rearranged to ensure that the
platform .o is always linked first. This means that platforms where
the zImage entry point is at a fixed address or offset, rather than
being encoded in the binary header can be supported using option (1).
Signed-off-by: David Gibson <dwg@au1.ibm.com>
Signed-off-by: Paul Mackerras <paulus@samba.org>
2007-03-05 11:24:52 +08:00
|
|
|
loader_info.promptr = promptr;
|
2007-04-13 08:46:21 +08:00
|
|
|
if (a1 && a2 && a2 != 0xdeadbeef) {
|
|
|
|
loader_info.initrd_addr = a1;
|
|
|
|
loader_info.initrd_size = a2;
|
|
|
|
}
|
2006-09-19 12:05:08 +08:00
|
|
|
}
|