Initial import of Leif's work
This commit is contained in:
parent
21026747df
commit
389b31cd71
65 changed files with 7311 additions and 13 deletions
112
grub-core/kern/uboot/hw.c
Normal file
112
grub-core/kern/uboot/hw.c
Normal file
|
@ -0,0 +1,112 @@
|
|||
/* hw.c - U-Boot hardware discovery */
|
||||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <grub/kernel.h>
|
||||
#include <grub/memory.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/offsets.h>
|
||||
#include <grub/machine/kernel.h>
|
||||
#include <grub/uboot/disk.h>
|
||||
#include <grub/uboot/uboot.h>
|
||||
|
||||
grub_addr_t start_of_ram;
|
||||
|
||||
/*
|
||||
* grub_uboot_probe_memory():
|
||||
* Queries U-Boot for available memory regions.
|
||||
*
|
||||
* Sets up heap near the image in memory and sets up "start_of_ram".
|
||||
*/
|
||||
void
|
||||
grub_uboot_mm_init (void)
|
||||
{
|
||||
struct sys_info *si = uboot_get_sys_info ();
|
||||
|
||||
grub_mm_init_region ((void *) (grub_modules_get_end ()
|
||||
+ GRUB_KERNEL_MACHINE_STACK_SIZE),
|
||||
GRUB_KERNEL_MACHINE_HEAP_SIZE);
|
||||
|
||||
if (si && (si->mr_no != 0))
|
||||
{
|
||||
int i;
|
||||
start_of_ram = GRUB_UINT_MAX;
|
||||
|
||||
for (i = 0; i < si->mr_no; i++)
|
||||
if ((si->mr[i].flags & MR_ATTR_MASK) == MR_ATTR_DRAM)
|
||||
if (si->mr[i].start < start_of_ram)
|
||||
start_of_ram = si->mr[i].start;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* grub_uboot_probe_hardware():
|
||||
*
|
||||
*/
|
||||
grub_err_t
|
||||
grub_uboot_probe_hardware (void)
|
||||
{
|
||||
int devcount, i;
|
||||
|
||||
devcount = uboot_dev_enum ();
|
||||
grub_dprintf ("init", "%d devices found\n", devcount);
|
||||
|
||||
for (i = 0; i < devcount; i++)
|
||||
{
|
||||
struct device_info *devinfo = uboot_dev_get (i);
|
||||
|
||||
grub_dprintf ("init", "device handle: %d\n", i);
|
||||
grub_dprintf ("init", " cookie\t= 0x%08x\n",
|
||||
(grub_uint32_t) devinfo->cookie);
|
||||
|
||||
if (devinfo->type & DEV_TYP_STOR)
|
||||
{
|
||||
grub_dprintf ("init", " type\t\t= DISK\n");
|
||||
grub_ubootdisk_register (devinfo, i);
|
||||
}
|
||||
else if (devinfo->type & DEV_TYP_NET)
|
||||
{
|
||||
grub_dprintf ("init", " type\t\t= NET (not supported yet)\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
grub_dprintf ("init", "%s: unknown device type", __FUNCTION__);
|
||||
}
|
||||
}
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
|
||||
{
|
||||
int i;
|
||||
struct sys_info *si = uboot_get_sys_info ();
|
||||
|
||||
if (!si || (si->mr_no < 1))
|
||||
return GRUB_ERR_BUG;
|
||||
|
||||
/* Iterate and call `hook'. */
|
||||
for (i = 0; i < si->mr_no; i++)
|
||||
if ((si->mr[i].flags & MR_ATTR_MASK) == MR_ATTR_DRAM)
|
||||
hook (si->mr[i].start, si->mr[i].size, GRUB_MEMORY_AVAILABLE,
|
||||
hook_data);
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
171
grub-core/kern/uboot/init.c
Normal file
171
grub-core/kern/uboot/init.c
Normal file
|
@ -0,0 +1,171 @@
|
|||
/* init.c - generic U-Boot initialization and finalization */
|
||||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include <grub/env.h>
|
||||
#include <grub/kernel.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/offsets.h>
|
||||
#include <grub/term.h>
|
||||
#include <grub/time.h>
|
||||
#include <grub/machine/kernel.h>
|
||||
#include <grub/uboot/console.h>
|
||||
#include <grub/uboot/disk.h>
|
||||
#include <grub/uboot/uboot.h>
|
||||
|
||||
extern char __bss_start[];
|
||||
extern char _end[];
|
||||
extern grub_size_t grub_total_module_size;
|
||||
extern int (*uboot_syscall_ptr) (int, int *, ...);
|
||||
|
||||
grub_addr_t grub_modbase;
|
||||
|
||||
grub_uint32_t uboot_machine_type;
|
||||
grub_addr_t uboot_boot_data;
|
||||
|
||||
static unsigned long timer_start;
|
||||
|
||||
void
|
||||
grub_exit (void)
|
||||
{
|
||||
uboot_return (0);
|
||||
}
|
||||
|
||||
grub_uint32_t
|
||||
uboot_get_machine_type (void)
|
||||
{
|
||||
return uboot_machine_type;
|
||||
}
|
||||
|
||||
grub_addr_t
|
||||
uboot_get_boot_data (void)
|
||||
{
|
||||
return uboot_boot_data;
|
||||
}
|
||||
|
||||
static grub_uint64_t
|
||||
uboot_timer_ms (void)
|
||||
{
|
||||
return (grub_uint64_t) uboot_get_timer (timer_start);
|
||||
}
|
||||
|
||||
void
|
||||
grub_machine_init (void)
|
||||
{
|
||||
grub_addr_t end, real_bss_start;
|
||||
int ver;
|
||||
|
||||
/* First of all - establish connection with U-Boot */
|
||||
ver = uboot_api_init ();
|
||||
if (!ver)
|
||||
{
|
||||
/* Don't even have a console to log errors to... */
|
||||
grub_exit ();
|
||||
}
|
||||
else if (ver > API_SIG_VERSION)
|
||||
{
|
||||
/* Try to print an error message */
|
||||
uboot_puts ("invalid U-Boot API version\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* Modules were relocated to _end, or __bss_start + grub_total_module_size,
|
||||
* whichever greater. (And __bss_start may not point to actual BSS start...)
|
||||
*/
|
||||
real_bss_start = uboot_get_real_bss_start ();
|
||||
end = real_bss_start + grub_total_module_size;
|
||||
if (end < (grub_addr_t) _end)
|
||||
end = (grub_addr_t) _end;
|
||||
grub_modbase = end;
|
||||
|
||||
/* Initialize the console so that GRUB can display messages. */
|
||||
grub_console_init_early ();
|
||||
|
||||
/* Enumerate memory and initialize the memory management system. */
|
||||
grub_uboot_mm_init ();
|
||||
|
||||
grub_dprintf ("init", "__bss_start: 0x%08x, real_bss_start: 0x%08x\n",
|
||||
(grub_addr_t) __bss_start, real_bss_start);
|
||||
grub_dprintf ("init", "end: 0x%08x, _end: 0x%08x\n",
|
||||
(grub_addr_t) end, (grub_addr_t) _end);
|
||||
grub_dprintf ("init", "grub_modbase: %p\n", (void *) grub_modbase);
|
||||
grub_dprintf ("init", "grub_modules_get_end(): %p\n",
|
||||
(void *) grub_modules_get_end ());
|
||||
|
||||
/* Initialise full terminfo support */
|
||||
grub_console_init_lately ();
|
||||
|
||||
/* Enumerate uboot devices */
|
||||
grub_uboot_probe_hardware ();
|
||||
|
||||
/* Initialise timer */
|
||||
timer_start = uboot_get_timer (0);
|
||||
grub_install_get_time_ms (uboot_timer_ms);
|
||||
|
||||
/* Initialize */
|
||||
grub_ubootdisk_init ();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
grub_machine_fini (void)
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* grub_machine_get_bootlocation():
|
||||
* Called from kern/main.c, which expects a device name (minus parentheses)
|
||||
* and a filesystem path back, if any are known.
|
||||
* Any returned values must be pointers to dynamically allocated strings.
|
||||
*/
|
||||
void
|
||||
grub_machine_get_bootlocation (char **device, char **path)
|
||||
{
|
||||
char *tmp;
|
||||
|
||||
tmp = uboot_env_get ("grub_bootdev");
|
||||
if (tmp)
|
||||
{
|
||||
*device = grub_malloc (grub_strlen (tmp) + 1);
|
||||
if (*device == NULL)
|
||||
return;
|
||||
grub_strncpy (*device, tmp, grub_strlen (tmp) + 1);
|
||||
}
|
||||
else
|
||||
*device = NULL;
|
||||
|
||||
tmp = uboot_env_get ("grub_bootpath");
|
||||
if (tmp)
|
||||
{
|
||||
*path = grub_malloc (grub_strlen (tmp) + 1);
|
||||
if (*path == NULL)
|
||||
return;
|
||||
grub_strncpy (*path, tmp, grub_strlen (tmp) + 1);
|
||||
}
|
||||
else
|
||||
*path = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
grub_uboot_fini (void)
|
||||
{
|
||||
grub_ubootdisk_fini ();
|
||||
grub_console_fini ();
|
||||
}
|
363
grub-core/kern/uboot/uboot.c
Normal file
363
grub-core/kern/uboot/uboot.c
Normal file
|
@ -0,0 +1,363 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <grub/misc.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/uboot/uboot.h>
|
||||
|
||||
/*
|
||||
* The main syscall entry point is not reentrant, only one call is
|
||||
* serviced until finished.
|
||||
*
|
||||
* int syscall(int call, int *retval, ...)
|
||||
* e.g. syscall(1, int *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
|
||||
*
|
||||
* call: syscall number
|
||||
*
|
||||
* retval: points to the return value placeholder, this is the place the
|
||||
* syscall puts its return value, if NULL the caller does not
|
||||
* expect a return value
|
||||
*
|
||||
* ... syscall arguments (variable number)
|
||||
*
|
||||
* returns: 0 if the call not found, 1 if serviced
|
||||
*/
|
||||
|
||||
extern int (*uboot_syscall_ptr) (int, int *, ...);
|
||||
extern int uboot_syscall (int, int *, ...);
|
||||
extern grub_addr_t uboot_search_hint;
|
||||
|
||||
static struct sys_info uboot_sys_info;
|
||||
static struct mem_region uboot_mem_info[5];
|
||||
static struct device_info uboot_devices[6];
|
||||
static int num_devices;
|
||||
|
||||
int
|
||||
uboot_api_init (void)
|
||||
{
|
||||
struct api_signature *start, *end;
|
||||
struct api_signature *p;
|
||||
|
||||
if (uboot_search_hint)
|
||||
{
|
||||
/* Extended search range to work around Trim Slice U-Boot issue */
|
||||
start = (struct api_signature *) ((uboot_search_hint & ~0x000fffff)
|
||||
- 0x00500000);
|
||||
end =
|
||||
(struct api_signature *) ((grub_addr_t) start + UBOOT_API_SEARCH_LEN -
|
||||
API_SIG_MAGLEN + 0x00500000);
|
||||
}
|
||||
else
|
||||
{
|
||||
start = 0;
|
||||
end = (struct api_signature *) (256 * 1024 * 1024);
|
||||
}
|
||||
|
||||
/* Structure alignment is (at least) 8 bytes */
|
||||
for (p = start; p < end; p = (void *) ((grub_addr_t) p + 8))
|
||||
{
|
||||
if (grub_memcmp (&(p->magic), API_SIG_MAGIC, API_SIG_MAGLEN) == 0)
|
||||
{
|
||||
uboot_syscall_ptr = p->syscall;
|
||||
return p->version;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* All functions below are wrappers around the uboot_syscall() function */
|
||||
|
||||
/*
|
||||
* int API_getc(int *c)
|
||||
*/
|
||||
int
|
||||
uboot_getc (void)
|
||||
{
|
||||
int c;
|
||||
if (!uboot_syscall (API_GETC, NULL, &c))
|
||||
return -1;
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
/*
|
||||
* int API_tstc(int *c)
|
||||
*/
|
||||
int
|
||||
uboot_tstc (void)
|
||||
{
|
||||
int c;
|
||||
if (!uboot_syscall (API_TSTC, NULL, &c))
|
||||
return -1;
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
/*
|
||||
* int API_putc(char *ch)
|
||||
*/
|
||||
void
|
||||
uboot_putc (int c)
|
||||
{
|
||||
uboot_syscall (API_PUTC, NULL, &c);
|
||||
}
|
||||
|
||||
/*
|
||||
* int API_puts(const char *s)
|
||||
*/
|
||||
void
|
||||
uboot_puts (const char *s)
|
||||
{
|
||||
uboot_syscall (API_PUTS, NULL, s);
|
||||
}
|
||||
|
||||
/*
|
||||
* int API_reset(void)
|
||||
*/
|
||||
void
|
||||
uboot_reset (void)
|
||||
{
|
||||
uboot_syscall (API_RESET, NULL, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* int API_get_sys_info(struct sys_info *si)
|
||||
*
|
||||
* fill out the sys_info struct containing selected parameters about the
|
||||
* machine
|
||||
*/
|
||||
struct sys_info *
|
||||
uboot_get_sys_info (void)
|
||||
{
|
||||
int retval;
|
||||
|
||||
grub_memset (&uboot_sys_info, 0, sizeof (uboot_sys_info));
|
||||
grub_memset (&uboot_mem_info, 0, sizeof (uboot_mem_info));
|
||||
uboot_sys_info.mr = uboot_mem_info;
|
||||
uboot_sys_info.mr_no = sizeof (uboot_mem_info) / sizeof (struct mem_region);
|
||||
|
||||
if (uboot_syscall (API_GET_SYS_INFO, &retval, &uboot_sys_info))
|
||||
if (retval == 0)
|
||||
return &uboot_sys_info;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* int API_udelay(unsigned long *udelay)
|
||||
*/
|
||||
void
|
||||
uboot_udelay (grub_uint32_t usec)
|
||||
{
|
||||
uboot_syscall (API_UDELAY, NULL, &usec);
|
||||
}
|
||||
|
||||
/*
|
||||
* int API_get_timer(unsigned long *current, unsigned long *base)
|
||||
*/
|
||||
grub_uint32_t
|
||||
uboot_get_timer (grub_uint32_t base)
|
||||
{
|
||||
grub_uint32_t current;
|
||||
|
||||
if (!uboot_syscall (API_GET_TIMER, NULL, ¤t, &base))
|
||||
return 0;
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
/*
|
||||
* int API_dev_enum(struct device_info *)
|
||||
*
|
||||
*/
|
||||
int
|
||||
uboot_dev_enum (void)
|
||||
{
|
||||
int max;
|
||||
|
||||
grub_memset (&uboot_devices, 0, sizeof (uboot_devices));
|
||||
max = sizeof (uboot_devices) / sizeof (struct device_info);
|
||||
|
||||
/*
|
||||
* The API_DEV_ENUM call starts a fresh enumeration when passed a
|
||||
* struct device_info with a NULL cookie, and then depends on having
|
||||
* the prevoiusly enumerated device cookie "seeded" into the target
|
||||
* structure.
|
||||
*/
|
||||
if (!uboot_syscall (API_DEV_ENUM, NULL, &uboot_devices)
|
||||
|| uboot_devices[0].cookie == NULL)
|
||||
return 0;
|
||||
|
||||
for (num_devices = 1; num_devices < max; num_devices++)
|
||||
{
|
||||
uboot_devices[num_devices].cookie =
|
||||
uboot_devices[num_devices - 1].cookie;
|
||||
if (!uboot_syscall (API_DEV_ENUM, NULL, &uboot_devices[num_devices]))
|
||||
return 0;
|
||||
|
||||
/* When no more devices to enumerate, target cookie set to NULL */
|
||||
if (uboot_devices[num_devices].cookie == NULL)
|
||||
break;
|
||||
}
|
||||
|
||||
return num_devices;
|
||||
}
|
||||
|
||||
#define VALID_DEV(x) (((x) < num_devices) && ((x) >= 0))
|
||||
#define OPEN_DEV(x) (VALID_DEV(x) && (uboot_devices[(x)].state == DEV_STA_OPEN))
|
||||
|
||||
struct device_info *
|
||||
uboot_dev_get (int handle)
|
||||
{
|
||||
if (VALID_DEV (handle))
|
||||
return &uboot_devices[handle];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* int API_dev_open(struct device_info *)
|
||||
*/
|
||||
int
|
||||
uboot_dev_open (int handle)
|
||||
{
|
||||
struct device_info *dev;
|
||||
int retval;
|
||||
|
||||
if (!VALID_DEV (handle))
|
||||
return -1;
|
||||
|
||||
dev = &uboot_devices[handle];
|
||||
|
||||
if (!uboot_syscall (API_DEV_OPEN, &retval, dev))
|
||||
return -1;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*
|
||||
* int API_dev_close(struct device_info *)
|
||||
*/
|
||||
int
|
||||
uboot_dev_close (int handle)
|
||||
{
|
||||
struct device_info *dev;
|
||||
int retval;
|
||||
|
||||
if (!VALID_DEV (handle))
|
||||
return -1;
|
||||
|
||||
dev = &uboot_devices[handle];
|
||||
|
||||
if (!uboot_syscall (API_DEV_CLOSE, &retval, dev))
|
||||
return -1;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* int API_dev_read(struct device_info *di, void *buf, size_t *len,
|
||||
* unsigned long *start, size_t *act_len)
|
||||
*/
|
||||
int
|
||||
uboot_dev_read (int handle, void *buf, lbasize_t blocks,
|
||||
lbastart_t start, lbasize_t * real_blocks)
|
||||
{
|
||||
struct device_info *dev;
|
||||
int retval;
|
||||
|
||||
if (!OPEN_DEV (handle))
|
||||
return -1;
|
||||
|
||||
dev = &uboot_devices[handle];
|
||||
|
||||
if (!uboot_syscall (API_DEV_READ, &retval, dev, buf,
|
||||
&blocks, &start, real_blocks))
|
||||
return -1;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*
|
||||
* int API_dev_read(struct device_info *di, void *buf,
|
||||
* size_t *len, size_t *act_len)
|
||||
*/
|
||||
int
|
||||
uboot_dev_recv (int handle, void *buf, int size, int *real_size)
|
||||
{
|
||||
struct device_info *dev;
|
||||
int retval;
|
||||
|
||||
if (!OPEN_DEV (handle))
|
||||
return -1;
|
||||
|
||||
dev = &uboot_devices[handle];
|
||||
if (!uboot_syscall (API_DEV_READ, &retval, dev, buf, &size, real_size))
|
||||
return -1;
|
||||
|
||||
return retval;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Notice: this is for sending network packets only, as U-Boot does not
|
||||
* support writing to storage at the moment (12.2007)
|
||||
*
|
||||
* int API_dev_write(struct device_info *di, void *buf, int *len)
|
||||
*/
|
||||
int
|
||||
uboot_dev_send (int handle, void *buf, int size)
|
||||
{
|
||||
struct device_info *dev;
|
||||
int retval;
|
||||
|
||||
if (!OPEN_DEV (handle))
|
||||
return -1;
|
||||
|
||||
dev = &uboot_devices[handle];
|
||||
if (!uboot_syscall (API_DEV_WRITE, &retval, dev, buf, &size))
|
||||
return -1;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*
|
||||
* int API_env_get(const char *name, char **value)
|
||||
*/
|
||||
char *
|
||||
uboot_env_get (const char *name)
|
||||
{
|
||||
char *value;
|
||||
|
||||
if (!uboot_syscall (API_ENV_GET, NULL, name, &value))
|
||||
return NULL;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/*
|
||||
* int API_env_set(const char *name, const char *value)
|
||||
*/
|
||||
void
|
||||
uboot_env_set (const char *name, const char *value)
|
||||
{
|
||||
uboot_syscall (API_ENV_SET, NULL, name, value);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue