* [RFCv2] GRUB port for ARMv7-A U-Boot
@ 2012-11-20 18:19 Leif Lindholm
0 siblings, 0 replies; only message in thread
From: Leif Lindholm @ 2012-11-20 18:19 UTC (permalink / raw)
To: grub-devel
[-- Attachment #1: Type: text/plain, Size: 2712 bytes --]
This is an updated patch for enabling support for running under ARMv7-A
U-Boot, based on some of Colin's feedback during UDS.
This is somewhat better tested from a usage-standpoint, having added
necessary support to commands like grub-install.
The version contained in this patch is also available as revision 4613 from
lp:~leif-lindholm/linaro-grub/arm-uboot.
Basic build and usage documentation is being kept at
https://wiki.linaro.org/LEG/Kernel/GRUB
Third-party code
----------------
I have rewritten the U-Boot glue layer in as much of a clean-room way
as can be done with a fairly trivial piece of code that you've already read
through a lot. This also gets rid of the special crc32 code.
The api_public.h header (GPLv2+, (c) Rafal Jaworowski) is still included.
The standalone "libgcc" helper implementation remains - there is no safe
workaround for the __clear_cache() issue, and requiring a parallel embedded
toolchain specifically for building GRUB does not make sense.
Nevertheless, all of the included code is (c) FSF.
libfdt (see below).
FDT
---
I did look into reusing the openfirmware code for device-tree handling,
but found no actual handling code in there. All actual scanning and
manipulation is performed through callbacks into openfirmware (using
IEEE1275_CALL_ENTRY_FN()).
So now I have tried instead to import libfdt the way gcrypt and Unicode is
imported. It generates a grub-core/Makefile.libfdt.def exporting a library
which is for now used only by the Linux loader.
Other
-----
The hack of calling uboot-mkimage from within grub-mkimage is now gone -
this is done more correctly by grub-install instead.
grub-install uses the UUID "trick" to make Grub find its modules now,
alleviating the need for special U-Boot environment variables.
Limitations
-----------
- Dynamic linker implemented only for ARMv7-A Thumb instruction set.
- Currently runs uncached, with MMU off.
- No support for ATAG parsing or rewriting, only supports device tree
as a means of passing information to the Linux kernel. (If an ATAG
list is passed from U-Boot, it can be passed on unmodified, but the
"linux" command will not pass on command line parameters.)
- Currently a statically linked kernel, requires RAM at 0x08000000.
- No write support for block devices. Not provided by U-Boot.
Missing
-------
Still, the testbench support and qemu configurations for grub-shell and its
dependents.
grub-mkrescue
Potential future developments
-----------------------------
- Implement networking support.
- Make GRUB kernel a fully relocateable ELF.
- Implement chainloader.
- Enable caches/MMU.
Comments welcome.
[-- Attachment #2: armv7-uboot-v2.patch --]
[-- Type: application/octet-stream, Size: 222293 bytes --]
diff -purN trunk/autogen.sh arm-uboot/autogen.sh
--- trunk/autogen.sh 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/autogen.sh 2012-11-20 16:26:54.950920000 +0000
@@ -14,6 +14,9 @@ python util/import_unicode.py unicode/Un
echo "Importing libgcrypt..."
python util/import_gcry.py grub-core/lib/libgcrypt/ grub-core
+echo "Importing libfdt..."
+python util/import_libfdt.py grub-core/lib/dtc/ grub-core
+
echo "Creating Makefile.tpl..."
python gentpl.py | sed -e '/^$/{N;/^\n$/D;}' > Makefile.tpl
@@ -27,7 +30,7 @@ if [ "x${GRUB_CONTRIB}" != x ]; then
fi
UTIL_DEFS='Makefile.util.def Makefile.utilgcry.def'
-CORE_DEFS='grub-core/Makefile.core.def grub-core/Makefile.gcry.def'
+CORE_DEFS='grub-core/Makefile.core.def grub-core/Makefile.gcry.def grub-core/Makefile.libfdt.def'
for extra in contrib/*/Makefile.util.def; do
if test -e "$extra"; then
diff -purN trunk/.bzrignore arm-uboot/.bzrignore
--- trunk/.bzrignore 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/.bzrignore 2012-11-20 16:26:54.950920000 +0000
@@ -128,6 +128,7 @@ depcomp
mdate-sh
texinfo.tex
grub-core/lib/libgcrypt-grub
+grub-core/lib/dtc-grub
**/.deps
**/.deps-util
**/.deps-core
@@ -136,6 +137,7 @@ Makefile.util.am
contrib
grub-core/Makefile.core.am
grub-core/Makefile.gcry.def
+grub-core/Makefile.libfdt.def
grub-core/contrib
grub-core/gdb_grub
grub-core/genmod.sh
diff -purN trunk/conf/Makefile.common arm-uboot/conf/Makefile.common
--- trunk/conf/Makefile.common 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/conf/Makefile.common 2012-11-20 16:26:54.950920000 +0000
@@ -37,6 +37,12 @@ if COND_sparc64_ieee1275
CFLAGS_PLATFORM += -mno-app-regs
LDFLAGS_PLATFORM = -Wl,-melf64_sparc -mno-relax
endif
+if COND_arm
+# Image entry point always in ARM (A32) state - ensure proper functionality if
+# the rest is built for the Thumb (T32) state.
+ CFLAGS_PLATFORM += -mthumb-interwork -mno-unaligned-access
+ CCASFLAGS_PLATFORM = -Wa,-mimplicit-it=thumb
+endif
# Other options
@@ -58,6 +64,7 @@ if ! COND_i386_coreboot
if ! COND_i386_multiboot
if ! COND_i386_ieee1275
if ! COND_x86_64_efi
+if ! COND_arm
LDADD_KERNEL += -lgcc
endif
endif
@@ -66,9 +73,14 @@ endif
endif
endif
endif
+endif
CFLAGS_KERNEL = $(CFLAGS_CPU) $(CFLAGS_PLATFORM) -ffreestanding
+if ! COND_arm
LDFLAGS_KERNEL = $(LDFLAGS_CPU) $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) -static-libgcc
+else
+LDFLAGS_KERNEL = $(LDFLAGS_CPU) $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC)
+endif
CPPFLAGS_KERNEL = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM)
CCASFLAGS_KERNEL = $(CCASFLAGS_CPU) $(CCASFLAGS_PLATFORM)
if COND_CYGWIN
@@ -108,6 +120,8 @@ CPPFLAGS_GCRY = -I$(top_srcdir)/grub-cor
CFLAGS_GNULIB = -Wno-undef -Wno-sign-compare -Wno-unused -Wno-unused-parameter -Wno-redundant-decls -Wno-unreachable-code -Wno-conversion -Wno-old-style-definition -Wno-unsafe-loop-optimizations
CPPFLAGS_GNULIB = -I$(top_builddir)/grub-core/gnulib -I$(top_srcdir)/grub-core/gnulib
+CPPFLAGS_LIBFDT = -I$(top_srcdir)/grub-core/lib/dtc-grub/libfdt
+
CFLAGS_POSIX = -fno-builtin
CPPFLAGS_POSIX = -I$(top_srcdir)/grub-core/lib/posix_wrap
diff -purN trunk/configure.ac arm-uboot/configure.ac
--- trunk/configure.ac 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/configure.ac 2012-11-20 16:26:54.950920000 +0000
@@ -79,6 +79,9 @@ case "$target_cpu" in
target_cpu=mips;
machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_CPU_MIPS=1";
;;
+ arm*)
+ target_cpu=arm;
+ ;;
esac
# Specify the platform (such as firmware).
@@ -99,6 +102,7 @@ if test "x$with_platform" = x; then
mipsel-*) platform=loongson ;;
mips-*) platform=arc ;;
ia64-*) platform=efi ;;
+ arm-*) platform=uboot ;;
*) AC_MSG_ERROR([unsupported CPU: "$target_cpu"]) ;;
esac
else
@@ -133,6 +137,7 @@ case "$target_cpu"-"$platform" in
mipsel-yeeloong) platform=loongson ;;
mipsel-fuloong) platform=loongson ;;
mipsel-loongson) ;;
+ arm-uboot) platform=uboot ;;
*-emu) ;;
*) AC_MSG_ERROR([platform "$platform" is not supported for target CPU "$target_cpu"]) ;;
esac
@@ -164,6 +169,7 @@ case "$platform" in
multiboot) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MULTIBOOT=1" ;;
efi) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_EFI=1" ;;
ieee1275) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_IEEE1275=1" ;;
+ uboot) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_UBOOT=1" ;;
qemu) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_QEMU=1" ;;
pc) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_PCBIOS=1" ;;
emu) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_EMU=1" ;;
@@ -172,6 +178,7 @@ case "$platform" in
arc) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_ARC=1" ;;
esac
case "$target_cpu" in
+ arm) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_ARM=1" ;;
mips |mipsel) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MIPS=1" ;;
sparc64) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_SPARC64=1" ;;
esac
@@ -669,7 +676,8 @@ else
CFLAGS="$TARGET_CFLAGS -nostdlib -Wl,--defsym,___main=0x8100 -Wno-error"
fi
CPPFLAGS="$TARGET_CPPFLAGS"
-if test x$target_cpu = xi386 || test x$target_cpu = xx86_64 ; then
+
+if test x$target_cpu = xi386 || test x$target_cpu = xx86_64 || test x$target_cpu = xarm ; then
LIBS=
else
LIBS=-lgcc
@@ -1116,6 +1124,8 @@ AM_CONDITIONAL([COND_powerpc_ieee1275],
AM_CONDITIONAL([COND_mips], [test x$target_cpu = xmips -o x$target_cpu = xmipsel])
AM_CONDITIONAL([COND_mipsel], [test x$target_cpu = xmipsel])
AM_CONDITIONAL([COND_mipseb], [test x$target_cpu = xmips])
+AM_CONDITIONAL([COND_arm], [test x$target_cpu = xarm ])
+AM_CONDITIONAL([COND_arm_uboot], [test x$target_cpu = xarm -a x$platform = xuboot])
AM_CONDITIONAL([COND_HOST_HURD], [test x$host_kernel = xhurd])
AM_CONDITIONAL([COND_HOST_LINUX], [test x$host_kernel = xlinux])
diff -purN trunk/gentpl.py arm-uboot/gentpl.py
--- trunk/gentpl.py 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/gentpl.py 2012-11-20 16:26:54.950920000 +0000
@@ -23,7 +23,7 @@ GRUB_PLATFORMS = [ "emu", "i386_pc", "i3
"i386_multiboot", "i386_ieee1275", "x86_64_efi",
"mips_loongson", "sparc64_ieee1275",
"powerpc_ieee1275", "mips_arc", "ia64_efi",
- "mips_qemu_mips" ]
+ "mips_qemu_mips", "arm_uboot" ]
GROUPS = {}
@@ -36,10 +36,12 @@ GROUPS["x86"] = GROUPS["i386"] + GR
GROUPS["mips"] = [ "mips_loongson", "mips_qemu_mips", "mips_arc" ]
GROUPS["sparc64"] = [ "sparc64_ieee1275" ]
GROUPS["powerpc"] = [ "powerpc_ieee1275" ]
+GROUPS["arm"] = [ "arm_uboot" ]
# Groups based on firmware
GROUPS["efi"] = [ "i386_efi", "x86_64_efi", "ia64_efi" ]
GROUPS["ieee1275"] = [ "i386_ieee1275", "sparc64_ieee1275", "powerpc_ieee1275" ]
+GROUPS["uboot"] = [ "arm_uboot" ]
# emu is a special case so many core functionality isn't needed on this platform
GROUPS["noemu"] = GRUB_PLATFORMS[:]; GROUPS["noemu"].remove("emu")
@@ -57,10 +59,13 @@ GROUPS["videomodules"] = GRUB_PLATFORM
for i in GROUPS["videoinkernel"]: GROUPS["videomodules"].remove(i)
# Similar for terminfo
-GROUPS["terminfoinkernel"] = ["mips_loongson", "mips_arc", "mips_qemu_mips" ] + GROUPS["ieee1275"];
+GROUPS["terminfoinkernel"] = ["mips_loongson", "mips_arc", "mips_qemu_mips" ] + GROUPS["ieee1275"] + GROUPS["uboot"];
GROUPS["terminfomodule"] = GRUB_PLATFORMS[:];
for i in GROUPS["terminfoinkernel"]: GROUPS["terminfomodule"].remove(i)
+# Flattened Device Trees (FDT)
+GROUPS["fdt"] = [ "arm_uboot" ]
+
# Miscelaneous groups schedulded to disappear in future
GROUPS["i386_coreboot_multiboot_qemu"] = ["i386_coreboot", "i386_multiboot", "i386_qemu"]
GROUPS["nopc"] = GRUB_PLATFORMS[:]; GROUPS["nopc"].remove("i386_pc")
diff -purN trunk/grub-core/disk/uboot/ubootdisk.c arm-uboot/grub-core/disk/uboot/ubootdisk.c
--- trunk/grub-core/disk/uboot/ubootdisk.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/disk/uboot/ubootdisk.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,345 @@
+/* ubootdisk.c - disk subsystem support for U-Boot platforms */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/disk.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/partition.h>
+#include <grub/term.h>
+#include <grub/types.h>
+#include <grub/uboot/disk.h>
+#include <grub/uboot/uboot.h>
+
+static struct ubootdisk_data *fd_devices;
+static struct ubootdisk_data *hd_devices;
+static struct ubootdisk_data *cd_devices;
+
+/*
+ * grub_ubootdisk_register():
+ * Called for each disk device enumerated as part of U-Boot initialization
+ * code.
+ */
+grub_err_t
+grub_ubootdisk_register (struct device_info *newdev, int handle)
+{
+ struct ubootdisk_data *d;
+ enum disktype type;
+
+#define STOR_TYPE(x) ((x) & 0x0ff0)
+ switch (STOR_TYPE (newdev->type))
+ {
+ case DT_STOR_IDE:
+ case DT_STOR_SATA:
+ /* hd */
+ type = hd;
+ break;
+ case DT_STOR_MMC:
+ case DT_STOR_USB:
+ /* fd */
+ type = fd;
+ break;
+ default:
+ return GRUB_ERR_BAD_DEVICE;
+ break;
+ }
+
+ d = (struct ubootdisk_data *) grub_malloc (sizeof (struct ubootdisk_data));
+ if (!d)
+ return GRUB_ERR_OUT_OF_MEMORY;
+ d->handle = handle;
+ d->cookie = newdev->cookie;
+ d->opencount = 0;
+
+ switch (type)
+ {
+ case cd:
+ grub_dprintf ("ubootdisk", "registering cd device\n");
+ d->next = cd_devices;
+ cd_devices = d;
+
+ break;
+ case fd:
+ grub_dprintf ("ubootdisk", "registering fd device\n");
+ d->next = fd_devices;
+ fd_devices = d;
+
+ break;
+ case hd:
+ grub_dprintf ("ubootdisk", "registering hd device\n");
+ d->next = hd_devices;
+ hd_devices = d;
+
+ break;
+ default:
+ grub_free (d);
+ return GRUB_ERR_BAD_DEVICE;
+ }
+
+ return 0;
+}
+
+/*
+ * uboot_disk_iterate():
+ * Itarator over enumerated disk devices.
+ */
+static int
+uboot_disk_iterate (int (*hook) (const char *name), grub_disk_pull_t pull)
+{
+ struct ubootdisk_data *d;
+ char buf[16];
+ int count;
+
+ switch (pull)
+ {
+ case GRUB_DISK_PULL_NONE:
+ /* "hd" - built-in mass-storage */
+ for (d = hd_devices, count = 0; d; d = d->next, count++)
+ {
+ grub_snprintf (buf, sizeof (buf) - 1, "hd%d", count);
+ grub_dprintf ("ubootdisk", "iterating %s\n", buf);
+ if (hook (buf))
+ return 1;
+ }
+ break;
+ case GRUB_DISK_PULL_REMOVABLE:
+ /* "floppy" - removable mass storage */
+ for (d = fd_devices, count = 0; d; d = d->next, count++)
+ {
+ grub_snprintf (buf, sizeof (buf) - 1, "fd%d", count);
+ grub_dprintf ("ubootdisk", "iterating %s\n", buf);
+ if (hook (buf))
+ return 1;
+ }
+
+ /* "cdrom" - removeable read-only storage */
+ for (d = cd_devices, count = 0; d; d = d->next, count++)
+ {
+ grub_snprintf (buf, sizeof (buf) - 1, "cd%d", count);
+ grub_dprintf ("ubootdisk", "iterating %s\n", buf);
+ if (hook (buf))
+ return 1;
+ }
+ break;
+ default:
+ return 0;
+ }
+
+ return 0;
+}
+
+/* Helper function for uboot_disk_open. */
+static struct ubootdisk_data *
+get_device (struct ubootdisk_data *devices, int num)
+{
+ struct ubootdisk_data *d;
+
+ for (d = devices; d && num; d = d->next, num--)
+ ;
+
+ if (num == 0)
+ return d;
+
+ return NULL;
+}
+
+/*
+ * uboot_disk_open():
+ * Opens a disk device already enumerated.
+ */
+static grub_err_t
+uboot_disk_open (const char *name, struct grub_disk *disk)
+{
+ struct ubootdisk_data *d;
+ struct device_info *devinfo;
+ int num;
+ int retval;
+
+ grub_dprintf ("ubootdisk", "Opening '%s'\n", name);
+
+ num = grub_strtoul (name + 2, 0, 10);
+ if (grub_errno != GRUB_ERR_NONE)
+ {
+ grub_dprintf ("ubootdisk", "Opening '%s' failed, invalid number\n",
+ name);
+ goto fail;
+ }
+
+ if (name[1] != 'd')
+ {
+ grub_dprintf ("ubootdisk", "Opening '%s' failed, invalid name\n", name);
+ goto fail;
+ }
+
+ switch (name[0])
+ {
+ case 'f':
+ d = get_device (fd_devices, num);
+ break;
+ case 'c':
+ d = get_device (cd_devices, num);
+ break;
+ case 'h':
+ d = get_device (hd_devices, num);
+ break;
+ default:
+ goto fail;
+ }
+
+ if (!d)
+ goto fail;
+
+ /*
+ * Subsystems may call open on the same device recursively - but U-Boot
+ * does not deal with this. So simply keep track of number of calls and
+ * return success if already open.
+ */
+ if (d->opencount > 0)
+ {
+ grub_dprintf ("ubootdisk", "(%s) already open\n", disk->name);
+ d->opencount++;
+ retval = 0;
+ }
+ else
+ {
+ retval = uboot_dev_open (d->handle);
+ if (retval != 0)
+ goto fail;
+ d->opencount = 1;
+ }
+
+ grub_dprintf ("ubootdisk", "cookie: 0x%08x\n", (grub_addr_t) d->cookie);
+ disk->id = (grub_addr_t) d->cookie;
+
+ /* Device has previously been enumerated, so this should never fail */
+ if ((devinfo = uboot_dev_get (d->handle)) == NULL)
+ goto fail;
+
+ d->block_size = devinfo->di_stor.block_size;
+ if (d->block_size == 0)
+ {
+ grub_printf ("%s: no block size!\n", __FUNCTION__);
+ return GRUB_ERR_IO;
+ }
+
+ for (disk->log_sector_size = 0;
+ (1U << disk->log_sector_size) < d->block_size;
+ disk->log_sector_size++);
+
+ grub_dprintf ("ubootdisk", "(%s) blocksize=%d, log_sector_size=%d\n",
+ disk->name, d->block_size, disk->log_sector_size);
+
+ disk->total_sectors = GRUB_DISK_SIZE_UNKNOWN;
+ disk->data = d;
+
+ return GRUB_ERR_NONE;
+
+fail:
+ return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such device");
+}
+
+static void
+uboot_disk_close (struct grub_disk *disk)
+{
+ struct ubootdisk_data *d;
+ int retval;
+
+ d = disk->data;
+
+ /*
+ * In mirror of open function, keep track of number of calls to close and
+ * send on to U-Boot only when opencount would decrease to 0.
+ */
+ if (d->opencount > 1)
+ {
+ grub_dprintf ("ubootdisk", "Closed (%s)\n", disk->name);
+
+ d->opencount--;
+ }
+ else if (d->opencount == 1)
+ {
+ retval = uboot_dev_close (d->handle);
+ d->opencount--;
+ grub_dprintf ("ubootdisk", "closed %s (%d)\n", disk->name, retval);
+ }
+ else
+ {
+ grub_dprintf ("ubootdisk", "device %s not open!\n", disk->name);
+ }
+}
+
+/*
+ * uboot_disk_read():
+ * Called from within disk subsystem to read a sequence of blocks into the
+ * disk cache. Maps directly on top of U-Boot API, only wrap in some error
+ * handling.
+ */
+static grub_err_t
+uboot_disk_read (struct grub_disk *disk,
+ grub_disk_addr_t offset, grub_size_t numblocks, char *buf)
+{
+ struct ubootdisk_data *d;
+ lbasize_t real_size;
+ int retval;
+
+ d = disk->data;
+
+ retval = uboot_dev_read (d->handle, buf, numblocks, offset, &real_size);
+ grub_dprintf ("ubootdisk",
+ "retval=%d, numblocks=%d, real_size=%llu, sector=%llu\n",
+ retval, numblocks, (grub_uint64_t) real_size,
+ (grub_uint64_t) offset);
+ if (retval != 0)
+ return grub_error (GRUB_ERR_IO, "U-Boot disk read error");
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+uboot_disk_write (struct grub_disk *disk __attribute__ ((unused)),
+ grub_disk_addr_t sector __attribute__ ((unused)),
+ grub_size_t size __attribute__ ((unused)),
+ const char *buf __attribute__ ((unused)))
+{
+ grub_dprintf ("ubootdisk", "attempt to write\n");
+ return GRUB_ERR_NOT_IMPLEMENTED_YET;
+}
+
+static struct grub_disk_dev grub_ubootdisk_dev = {
+ .name = "ubootdisk",
+ .id = GRUB_DISK_DEVICE_UBOOTDISK_ID,
+ .iterate = uboot_disk_iterate,
+ .open = uboot_disk_open,
+ .close = uboot_disk_close,
+ .read = uboot_disk_read,
+ .write = uboot_disk_write,
+ .next = 0
+};
+
+void
+grub_ubootdisk_init (void)
+{
+ grub_disk_dev_register (&grub_ubootdisk_dev);
+}
+
+void
+grub_ubootdisk_fini (void)
+{
+ grub_disk_dev_unregister (&grub_ubootdisk_dev);
+}
diff -purN trunk/grub-core/kern/arm/dl.c arm-uboot/grub-core/kern/arm/dl.c
--- trunk/grub-core/kern/arm/dl.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/kern/arm/dl.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,327 @@
+/* dl.c - arch-dependent part of loadable module support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/dl.h>
+#include <grub/elf.h>
+#include <grub/misc.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/i18n.h>
+
+#if !defined(__thumb2__)
+#error "Relocations not implemented for A32 ("ARM") instruction set yet!"
+#endif
+
+#ifdef DL_DEBUG
+static const char *symstrtab;
+
+/*
+ * This is a bit of a hack, setting the symstrtab pointer to the last STRTAB
+ * section in the module (which is where symbol names are in the objects I've
+ * inspected manually).
+ */
+static void
+set_symstrtab (Elf_Ehdr * e)
+{
+ int i;
+ Elf_Shdr *s;
+
+ symstrtab = NULL;
+
+ for (i = 0, s = (Elf_Shdr *) ((grub_uint32_t) e + e->e_shoff);
+ i < e->e_shnum;
+ i++, s = (Elf_Shdr *) ((grub_uint32_t) s + e->e_shentsize))
+ if (s->sh_type == SHT_STRTAB)
+ symstrtab = (void *) ((grub_addr_t) e + s->sh_offset);
+}
+
+static const char *
+get_symbolname (Elf_Sym * sym)
+{
+ const char *symbolname = symstrtab + sym->st_name;
+
+ return (*symbolname ? symbolname : NULL);
+}
+#endif /* DL_DEBUG */
+
+
+/*
+ * Simple relocation of 32-bit value (in literal pool)
+ */
+static grub_err_t
+reloc_abs32 (Elf_Word * addr, Elf_Sym * sym)
+{
+#ifdef DL_DEBUG
+ grub_printf ("%s: ABS32 @ 0x%08x -> %s @ 0x%08x\n",
+ __FUNCTION__, (grub_addr_t) addr,
+ get_symbolname (sym), sym->st_value);
+#endif
+
+ *addr += sym->st_value;
+
+ return GRUB_ERR_NONE;
+}
+
+/*
+ * R_ARM_THM_CALL/THM_JUMP24
+ *
+ * Deals with relocation of Thumb (T32) instruction set relative branches
+ * B.W, BL and BLX
+ *
+ * 32-bit Thumb instructions can be 16-bit aligned, and are fetched
+ * little-endian, requiring some additional fiddling.
+ */
+static grub_err_t
+reloc_thm_call (grub_uint16_t * addr, Elf_Sym * sym)
+{
+ grub_int32_t offset, offset_low, offset_high;
+ grub_uint32_t sign, j1, j2, is_blx;
+ grub_uint32_t insword, insmask;
+
+ /* Extract instruction word in alignment-safe manner */
+ insword = (*addr << 16) | *(addr + 1);
+ insmask = 0xf800d000;
+
+ /* B.W/BL or BLX? Affects range and expected target state */
+ if (((insword >> 12) & 0xd) == 0xc)
+ is_blx = 1;
+ else
+ is_blx = 0;
+
+ offset_low = -16777216;
+ offset_high = is_blx ? 16777212 : 16777214;
+
+#ifdef DL_DEBUG
+ grub_printf ("%s: %s @ 0x%08x -> %s @ 0x%08x\n",
+ __FUNCTION__, is_blx ? "BLX" : "B(L)",
+ (grub_addr_t) addr, get_symbolname (sym), sym->st_value);
+#endif
+
+ /* Extract bitfields from instruction words */
+ sign = (insword >> 26) & 1;
+ j1 = (insword >> 13) & 1;
+ j2 = (insword >> 11) & 1;
+ offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
+ ((~(j2 ^ sign) & 1) << 22) |
+ ((insword & 0x03ff0000) >> 4) | ((insword & 0x000007ff) << 1);
+
+ /* Sign adjust and calculate offset */
+ if (offset & 0x01000000)
+ offset -= 0x02000000;
+ offset += sym->st_value - (grub_uint32_t) addr;
+
+ if ((offset < offset_low) || (offset > offset_high))
+ return grub_error (GRUB_ERR_OUT_OF_RANGE,
+ N_("offset %d (0x%08x) is out of range"),
+ offset, offset);
+
+ /* If BLX, target symbol must be ARM (target address LSB == 0) */
+ if (is_blx && (offset & 1))
+ return grub_error
+ (GRUB_ERR_BUG, N_("Relocation targeting wrong execution state"));
+
+ /* Reassemble instruction word */
+ sign = (offset >> 24) & 1;
+ j1 = sign ^ (~(offset >> 23) & 1);
+ j2 = sign ^ (~(offset >> 22) & 1);
+ insword = (insword & insmask) |
+ (sign << 26) |
+ (((offset >> 12) & 0x03ff) << 16) |
+ (j1 << 13) | (j2 << 11) | ((offset >> 1) & 0x07ff);
+
+ /* Write instruction word back in alignment-safe manner */
+ *(grub_uint16_t *) addr = (insword >> 16) & 0xffff;
+ *(grub_uint16_t *) (addr + 1) = insword & 0xffff;
+
+ return GRUB_ERR_NONE;
+}
+
+/*
+ * find_segment(): finds a module segment matching sh_info
+ */
+static grub_dl_segment_t
+find_segment (grub_dl_segment_t seg, Elf32_Word sh_info)
+{
+ for (; seg; seg = seg->next)
+ if (seg->section == sh_info)
+ return seg;
+
+ return NULL;
+}
+
+/*
+ * do_relocations():
+ * Iterate over all relocations in section, calling appropriate functions
+ * for patching.
+ */
+static grub_err_t
+do_relocations (Elf_Shdr * relhdr, Elf_Ehdr * e, grub_dl_t mod)
+{
+ grub_dl_segment_t seg;
+ Elf_Rel *rel;
+ Elf_Sym *sym;
+ int i, entnum;
+
+ entnum = relhdr->sh_size / sizeof (Elf_Rel);
+
+ /* Find the target segment for this relocation section. */
+ seg = find_segment (mod->segment, relhdr->sh_info);
+ if (!seg)
+ return grub_error (GRUB_ERR_EOF, N_("relocation segment not found"));
+
+ rel = (Elf_Rel *) ((grub_addr_t) e + relhdr->sh_offset);
+
+ /* Step through all relocations */
+ for (i = 0, sym = mod->symtab; i < entnum; i++)
+ {
+ Elf_Word *addr;
+ int relsym, reltype;
+ grub_err_t retval;
+
+ if (seg->size < rel[i].r_offset)
+ return grub_error (GRUB_ERR_BAD_MODULE,
+ "reloc offset is out of the segment");
+ relsym = ELF_R_SYM (rel[i].r_info);
+ reltype = ELF_R_TYPE (rel[i].r_info);
+ addr = (Elf_Word *) ((grub_addr_t) seg->addr + rel[i].r_offset);
+
+ switch (reltype)
+ {
+ case R_ARM_ABS32:
+ {
+ /* Data will be naturally aligned */
+ retval = reloc_abs32 (addr, &sym[relsym]);
+ if (retval != GRUB_ERR_NONE)
+ return retval;
+ }
+ break;
+ case R_ARM_THM_CALL:
+ case R_ARM_THM_JUMP24:
+ {
+ /* Thumb instructions can be 16-bit aligned */
+ retval = reloc_thm_call ((grub_uint16_t *) addr, &sym[relsym]);
+ if (retval != GRUB_ERR_NONE)
+ return retval;
+ }
+ break;
+ default:
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("relocation 0x%x is not implemented yet"),
+ reltype);
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+
+/*
+ * Check if EHDR is a valid ELF header.
+ */
+grub_err_t
+grub_arch_dl_check_header (void *ehdr)
+{
+ Elf_Ehdr *e = ehdr;
+
+ /* Check the magic numbers. */
+ if (e->e_ident[EI_CLASS] != ELFCLASS32
+ || e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_ARM)
+ return grub_error (GRUB_ERR_BAD_OS,
+ N_("invalid arch-dependent ELF magic"));
+
+ return GRUB_ERR_NONE;
+}
+
+/*
+ * Verify that provided ELF header contains reference to a symbol table
+ */
+static int
+has_symtab (Elf_Ehdr * e)
+{
+ int i;
+ Elf_Shdr *s;
+
+ for (i = 0, s = (Elf_Shdr *) ((grub_uint32_t) e + e->e_shoff);
+ i < e->e_shnum;
+ i++, s = (Elf_Shdr *) ((grub_uint32_t) s + e->e_shentsize))
+ if (s->sh_type == SHT_SYMTAB)
+ return 1;
+
+ return 0;
+}
+
+/*
+ * grub_arch_dl_relocate_symbols():
+ * Only externally visible function in this file.
+ * Locates the relocations section of the ELF object, and calls
+ * do_relocations() to deal with it.
+ */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
+{
+ Elf_Ehdr *e = ehdr;
+ Elf_Shdr *s;
+ unsigned i;
+
+ if (!has_symtab (e))
+ return grub_error (GRUB_ERR_BAD_MODULE, N_("no symbol table"));
+
+#ifdef DL_DEBUG
+ set_symstrtab (e);
+#endif
+
+#define FIRST_SHDR(x) ((Elf_Shdr *) ((grub_addr_t)(x) + (x)->e_shoff))
+#define NEXT_SHDR(x, y) ((Elf_Shdr *) ((grub_addr_t)(y) + (x)->e_shentsize))
+
+ for (i = 0, s = FIRST_SHDR (e); i < e->e_shnum; i++, s = NEXT_SHDR (e, s))
+ {
+ grub_err_t ret;
+
+ switch (s->sh_type)
+ {
+ case SHT_REL:
+ {
+ /* Relocations, no addends */
+ ret = do_relocations (s, e, mod);
+ if (ret != GRUB_ERR_NONE)
+ return ret;
+ }
+ break;
+ case SHT_NULL:
+ case SHT_PROGBITS:
+ case SHT_SYMTAB:
+ case SHT_STRTAB:
+ case SHT_NOBITS:
+ case SHT_ARM_ATTRIBUTES:
+ break;
+ case SHT_RELA:
+ default:
+ {
+ grub_printf ("unhandled section_type: %d (0x%08x)\n",
+ s->sh_type, s->sh_type);
+ return GRUB_ERR_NOT_IMPLEMENTED_YET;
+ };
+ }
+ }
+
+#undef FIRST_SHDR
+#undef NEXT_SHDR
+
+ return GRUB_ERR_NONE;
+}
diff -purN trunk/grub-core/kern/arm/uboot/startup.S arm-uboot/grub-core/kern/arm/uboot/startup.S
--- trunk/grub-core/kern/arm/uboot/startup.S 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/kern/arm/uboot/startup.S 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,163 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/offsets.h>
+#include <grub/symbol.h>
+#include <grub/machine/kernel.h>
+
+/*
+ * GRUB is called from U-Boot as a Linux Kernel type image, which
+ * means among other things that it always enters in ARM state.
+ *
+ *
+ * Overview of GRUB image layout:
+ *
+ * _start:
+ * Entry point (1 ARM branch instruction, to "codestart")
+ * grub_total_module_size:
+ * Data field: Size of included module blob
+ * (when generated by grub-mkimage)
+ * codestart:
+ * Remainder of statically-linked executable code and data.
+ * __bss_start:
+ * Start of included module blob.
+ * Also where global/static variables are located.
+ * _end:
+ * End of bss region (but not necessarily module blob).
+ * <overflow>:
+ * Any part of the module blob that extends beyond _end.
+ * <modules>:
+ * Loadable modules, post relocation.
+ * <stack>:
+ * <heap>:
+ */
+
+ .text
+ .arm
+FUNCTION(_start)
+ b codestart
+
+ @ Size of final image integrated module blob - set by grub-mkimage
+ . = _start + GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE
+VARIABLE(grub_total_module_size)
+ .long 0
+
+FUNCTION(codestart)
+ @ Store context: Machine ID, atags/dtb, ...
+ @ U-Boot API signature is stored on the U-Boot heap
+ @ Stack pointer used as start address for signature probing
+ mov r12, sp
+ ldr sp, =entry_state
+ push {r4-r12,lr} @ store U-Boot context (sp in r12)
+
+ @ Put kernel parameters aside until we can store them (further down)
+ mov r4, r1 @ machine type
+ mov r5, r2 @ boot data
+
+ @ Modules have been stored as a blob in BSS,
+ @ they need to be manually relocated to _end or
+ @ (__bss_start + grub_total_module_size), whichever greater.
+ ldr r0, =EXT_C(__bss_start) @ src
+ ldr r1, =EXT_C(_end) @ dst = End of BSS
+ ldr r2, grub_total_module_size @ blob size
+ add r3, r0, r2 @ blob end
+ cmp r1, r3 @ _end < blob end?
+ movlt r1, r3 @ dst = blob end + blob size
+
+1: ldr r3, [r0], #4 @ r3 = *src++
+ str r3, [r1], #4 @ *dst++ = r3
+ subs r2, #4 @ remaining -= 4
+ bne 1b @ while remaining != 0
+
+ @ Set up a new stack, beyond the end of copied modules.
+ ldr r3, =GRUB_KERNEL_MACHINE_STACK_SIZE
+ add r3, r1, r3 @ Place stack beyond end of modules
+ and sp, r3, #~0x7 @ Ensure 8-byte alignment
+
+ @ Since we _are_ the C run-time, we need to manually zero the BSS
+ @ region before continuing
+ ldr r0, =EXT_C(__bss_start)
+ ldr r1, =EXT_C(_end)
+ mov r2, #0
+1: str r2, [r0], #4
+ cmp r0, r1
+ bne 1b
+
+ @ Global variables now accessible - store kernel parameters in memory
+ ldr r12, =EXT_C(uboot_machine_type)
+ str r4, [r12]
+ ldr r12, =EXT_C(uboot_boot_data)
+ str r5, [r12]
+
+ b grub_main
+
+ /*
+ * uboot_syscall():
+ * This function is effectively a veneer, so it cannot
+ * modify the stack or corrupt any registers other than
+ * r12 (ip). Furthermore it needs to restore r8 for
+ * U-Boot (Global Data Pointer) and preserve it for Grub.
+ */
+FUNCTION(uboot_syscall)
+ ldr ip, =transition_space
+ stm ip, {r8, lr}
+ ldr ip, =gd_backup
+ ldr r8, [ip]
+ ldr ip, =uboot_syscall_ptr
+ mov lr, pc
+ ldr pc, [ip]
+ ldr ip, =gd_backup
+ str r8, [ip]
+ ldr ip, =transition_space
+ ldm ip, {r8, lr}
+ bx lr
+
+FUNCTION(uboot_return)
+ ldr sp, =entry_state_end
+ pop {r4-r12, lr}
+ mov sp, r12
+ bx lr
+
+
+ .data
+ .align 3 @ 8-byte alignment for stack
+@ U-boot context stack space
+entry_state_end:
+ .long 0 @ r4
+ .long 0 @ r5
+ .long 0 @ r6
+ .long 0 @ r7
+gd_backup:
+ .long 0 @ r8 - U-Boot global data pointer
+ .long 0 @ r9
+ .long 0 @ r10
+ .long 0 @ r11
+VARIABLE(uboot_search_hint)@ U-Boot stack pointer -
+ .long 0 @ also API signature address hint.
+ .long 0 @ lr
+entry_state: @ backup for U-Boot context
+
+@ GRUB context stack space
+transition_space:
+ .long 0 @ r8
+ .long 0 @ lr
+
+VARIABLE(uboot_syscall_ptr)
+ .long 0 @
+
+ .end
diff -purN trunk/grub-core/kern/main.c arm-uboot/grub-core/kern/main.c
--- trunk/grub-core/kern/main.c 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/grub-core/kern/main.c 2012-11-20 16:26:54.950920000 +0000
@@ -30,8 +30,10 @@
#include <grub/reader.h>
#include <grub/parser.h>
-/* This is actualy platform-independant but used only on loongson and sparc. */
-#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS) || defined (GRUB_MACHINE_SPARC64)
+/* This is actualy platform-independant but used only on some platforms. */
+#if defined (GRUB_MACHINE_MIPS_LOONGSON) || \
+ defined (GRUB_MACHINE_MIPS_QEMU_MIPS) || defined (GRUB_MACHINE_SPARC64) || \
+ defined (GRUB_MACHINE_ARM)
grub_addr_t
grub_modules_get_end (void)
{
diff -purN trunk/grub-core/kern/uboot/hw.c arm-uboot/grub-core/kern/uboot/hw.c
--- trunk/grub-core/kern/uboot/hw.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/kern/uboot/hw.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,111 @@
+/* hw.c - U-Boot hardware discovery */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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)
+{
+ 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);
+
+ return GRUB_ERR_NONE;
+}
diff -purN trunk/grub-core/kern/uboot/init.c arm-uboot/grub-core/kern/uboot/init.c
--- trunk/grub-core/kern/uboot/init.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/kern/uboot/init.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,169 @@
+/* init.c - generic U-Boot initialization and finalization */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/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;
+ 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.
+ */
+ end = (grub_addr_t) __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 ();
+
+ grub_dprintf ("init", "__bss_start: 0x%08x, end: 0x%08x, _end: 0x%08x\n",
+ (grub_addr_t) __bss_start,
+ (grub_addr_t) __bss_start + grub_total_module_size,
+ (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 ());
+
+ /* Enumerate memory and initialize the memory management system. */
+ grub_uboot_mm_init ();
+
+ /* 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 ();
+}
diff -purN trunk/grub-core/kern/uboot/uboot.c arm-uboot/grub-core/kern/uboot/uboot.c
--- trunk/grub-core/kern/uboot/uboot.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/kern/uboot/uboot.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,363 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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);
+}
diff -purN trunk/grub-core/lib/arm/libgcc.S arm-uboot/grub-core/lib/arm/libgcc.S
--- trunk/grub-core/lib/arm/libgcc.S 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/arm/libgcc.S 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,299 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/symbol.h>
+#include <grub/dl.h>
+
+ .file "libgcc.S"
+ .syntax unified
+#if !defined (__thumb2__)
+ .arm
+#define ARM(x...) x
+#define THUMB(x...)
+#else
+ .thumb
+#define THUMB(x...) x
+#define ARM(x...)
+#endif
+
+
+#ifdef __ARMEB__
+#define al r1
+#define ah r0
+#else
+#define al r0
+#define ah r1
+#endif
+
+GRUB_MOD_LICENSE "GPLv3+"
+
+/*
+ * helper functions - __aeabi* with macros imported from Linux kernel:
+ * linux/arch/arm/lib/lib1funcs.S
+ * linux/arch/arm/lib/ashldi3.S
+ * linux/arch/arm/lib/ashrdi3.S
+ * linux/arch/arm/lib/lshrdi3.S
+ */
+
+/*
+ * Taken from linux/arch/arm/lib/lib1funcs.S
+ * Retaining only ARMv5+ code paths
+ */
+.macro ARM_DIV_BODY dividend, divisor, result, curbit
+ clz \curbit, \divisor
+ clz \result, \dividend
+ sub \result, \curbit, \result
+ mov \curbit, #1
+ mov \divisor, \divisor, lsl \result
+ mov \curbit, \curbit, lsl \result
+ mov \result, #0
+
+ @ Division loop
+1: cmp \dividend, \divisor
+ subhs \dividend, \dividend, \divisor
+ orrhs \result, \result, \curbit
+ cmp \dividend, \divisor, lsr #1
+ subhs \dividend, \dividend, \divisor, lsr #1
+ orrhs \result, \result, \curbit, lsr #1
+ cmp \dividend, \divisor, lsr #2
+ subhs \dividend, \dividend, \divisor, lsr #2
+ orrhs \result, \result, \curbit, lsr #2
+ cmp \dividend, \divisor, lsr #3
+ subhs \dividend, \dividend, \divisor, lsr #3
+ orrhs \result, \result, \curbit, lsr #3
+ cmp \dividend, #0 @ Early termination?
+ movnes \curbit, \curbit, lsr #4 @ No, any more bits to do?
+ movne \divisor, \divisor, lsr #4
+ bne 1b
+
+.endm
+
+.macro ARM_DIV2_ORDER divisor, order
+ clz \order, \divisor
+ rsb \order, \order, #31
+.endm
+
+.macro ARM_MOD_BODY dividend, divisor, order, spare
+ clz \order, \divisor
+ clz \spare, \dividend
+ sub \order, \order, \spare
+ mov \divisor, \divisor, lsl \order
+ @ Perform all needed substractions to keep only the reminder.
+ @ Do comparisons in batch of 4 first.
+ subs \order, \order, #3 @ yes, 3 is intended here
+ blt 2f
+
+1: cmp \dividend, \divisor
+ subhs \dividend, \dividend, \divisor
+ cmp \dividend, \divisor, lsr #1
+ subhs \dividend, \dividend, \divisor, lsr #1
+ cmp \dividend, \divisor, lsr #2
+ subhs \dividend, \dividend, \divisor, lsr #2
+ cmp \dividend, \divisor, lsr #3
+ subhs \dividend, \dividend, \divisor, lsr #3
+ cmp \dividend, #1
+ mov \divisor, \divisor, lsr #4
+ subges \order, \order, #4
+ bge 1b
+
+ tst \order, #3
+ teqne \dividend, #0
+ beq 5f
+
+ @ Either 1, 2 or 3 comparison/substractions are left.
+2: cmn \order, #2
+ blt 4f
+ beq 3f
+ cmp \dividend, \divisor
+ subhs \dividend, \dividend, \divisor
+ mov \divisor, \divisor, lsr #1
+3: cmp \dividend, \divisor
+ subhs \dividend, \dividend, \divisor
+ mov \divisor, \divisor, lsr #1
+4: cmp \dividend, \divisor
+ subhs \dividend, \dividend, \divisor
+5:
+.endm
+
+ .text
+FUNCTION(__aeabi_uidiv)
+ subs r2, r1, #1
+ moveq pc, lr
+ bcc Ldiv0
+ cmp r0, r1
+ bls 11f
+ tst r1, r2
+ beq 12f
+
+ ARM_DIV_BODY r0, r1, r2, r3
+
+ mov r0, r2
+ mov pc, lr
+
+11: moveq r0, #1
+ movne r0, #0
+ bx lr
+
+12: ARM_DIV2_ORDER r1, r2
+
+ mov r0, r0, lsr r2
+ bx lr
+
+FUNCTION(__aeabi_idiv)
+ cmp r1, #0
+ eor ip, r0, r1 @ save the sign of the result.
+ beq Ldiv0
+ rsbmi r1, r1, #0 @ loops below use unsigned.
+ subs r2, r1, #1 @ division by 1 or -1 ?
+ beq 10f
+ movs r3, r0
+ rsbmi r3, r0, #0 @ positive dividend value
+ cmp r3, r1
+ bls 11f
+ tst r1, r2 @ divisor is power of 2 ?
+ beq 12f
+
+ ARM_DIV_BODY r3, r1, r0, r2
+
+ cmp ip, #0
+ rsbmi r0, r0, #0
+ bx lr
+
+10: teq ip, r0 @ same sign ?
+ rsbmi r0, r0, #0
+ bx lr
+
+11: movlo r0, #0
+ moveq r0, ip, asr #31
+ orreq r0, r0, #1
+ bx lr
+
+12: ARM_DIV2_ORDER r1, r2
+
+ cmp ip, #0
+ mov r0, r3, lsr r2
+ rsbmi r0, r0, #0
+ bx lr
+
+FUNCTION(__aeabi_uidivmod)
+ stmfd sp!, {r0, r1, ip, lr}
+ bl __aeabi_uidiv
+ ldmfd sp!, {r1, r2, ip, lr}
+ mul r3, r0, r2
+ sub r1, r1, r3
+ bx lr
+
+FUNCTION(__aeabi_idivmod)
+ stmfd sp!, {r0, r1, ip, lr}
+ bl __aeabi_idiv
+ ldmfd sp!, {r1, r2, ip, lr}
+ mul r3, r0, r2
+ sub r1, r1, r3
+ bx lr
+
+@ (Don't) handle division by 0
+FUNCTION(Ldiv0)
+ push {r4, lr}
+ mov r0, #0 @ About as wrong as it could be.
+ pop {r4, pc}
+
+
+/*
+ * From linux/arch/arm/lib/?sh*3.S
+ */
+FUNCTION(__aeabi_lasr)
+ subs r3, r2, #32
+ rsb ip, r2, #32
+ movmi al, al, lsr r2
+ movpl al, ah, asr r3
+ ARM( orrmi al, al, ah, lsl ip )
+ THUMB( lslmi r3, ah, ip )
+ THUMB( orrmi al, al, r3 )
+ mov ah, ah, asr r2
+ bx lr
+
+FUNCTION(__aeabi_llsl)
+ subs r3, r2, #32
+ rsb ip, r2, #32
+ movmi ah, ah, lsl r2
+ movpl ah, al, lsl r3
+ ARM( orrmi ah, ah, al, lsr ip )
+ THUMB( lsrmi r3, al, ip )
+ THUMB( orrmi ah, ah, r3 )
+ mov al, al, lsl r2
+ bx lr
+
+FUNCTION(__aeabi_llsr)
+ subs r3, r2, #32
+ rsb ip, r2, #32
+ movmi al, al, lsr r2
+ movpl al, ah, lsr r3
+ ARM( orrmi al, al, ah, lsl ip )
+ THUMB( lslmi r3, ah, ip )
+ THUMB( orrmi al, al, r3 )
+ mov ah, ah, lsr r2
+ bx lr
+
+
+/*
+ * Simple cache maintenance functions
+ */
+
+@ r0 - *beg (inclusive)
+@ r1 - *end (exclusive)
+clean_dcache_range:
+ @DCCMVAU
+1: cmp r0, r1
+ bge 2f
+ mcr p15, 0, r0, c7, c11, 1
+ add r0, r0, #32 @ assume 32-byte cache line
+2: dsb
+ bx lr
+
+@ r0 - *beg (inclusive)
+@ r1 - *end (exclusive)
+invalidate_icache_range:
+ @ICIMVAU
+1: cmp r0, r1
+ bge 2f
+ mcr p15, 0, r0, c7, c5, 1
+ @BPIMVA
+ mcr p15, 0, r0, c7, c5, 7
+ add r0, r0, #32 @ assume 32-byte cache line
+ b 1b
+ dsb
+2: isb
+ bx lr
+
+@void __clear_cache(char *beg, char *end);
+FUNCTION(__clear_cache)
+ push {r4-r6, lr}
+ mov r4, r0
+ mov r5, r1
+ bl clean_dcache_range
+ mov r0, r4
+ mov r1, r5
+ bl invalidate_icache_range
+ pop {r4-r6, pc}
+
+@void grub_arch_sync_caches (void *address, grub_size_t len)
+FUNCTION(grub_arch_sync_caches)
+ add r1, r0, r1
+ b __clear_cache
+
+ .end
diff -purN trunk/grub-core/lib/arm/setjmp.S arm-uboot/grub-core/lib/arm/setjmp.S
--- trunk/grub-core/lib/arm/setjmp.S 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/arm/setjmp.S 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,57 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/symbol.h>
+#include <grub/dl.h>
+
+ .file "setjmp.S"
+ .syntax unified
+#if !defined (__thumb2__)
+ .arm
+#define ARM(x...) x
+#define THUMB(x...)
+#else
+ .thumb
+#define THUMB(x...) x
+#define ARM(x...)
+#endif
+
+GRUB_MOD_LICENSE "GPLv3+"
+
+ .text
+
+/*
+ * int grub_setjmp (grub_jmp_buf env)
+ */
+FUNCTION(grub_setjmp)
+ THUMB( mov ip, sp )
+ THUMB( stm r0, { r4-r11, ip, lr } )
+ ARM( stm r0, { r4-r11, sp, lr } )
+ mov r0, #0
+ bx lr
+
+/*
+ * int grub_longjmp (grub_jmp_buf env, int val)
+ */
+FUNCTION(grub_longjmp)
+ THUMB( ldm r0, { r4-r11, ip, lr } )
+ THUMB( mov sp, ip )
+ ARM( ldm r0, { r4-r11, sp, lr } )
+ movs r0, r1
+ moveq r0, #1
+ bx lr
diff -purN trunk/grub-core/lib/dtc/libfdt/fdt.c arm-uboot/grub-core/lib/dtc/libfdt/fdt.c
--- trunk/grub-core/lib/dtc/libfdt/fdt.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/fdt.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,222 @@
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library 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.
+ *
+ * This library 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 library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+int fdt_check_header(const void *fdt)
+{
+ if (fdt_magic(fdt) == FDT_MAGIC) {
+ /* Complete tree */
+ if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
+ return -FDT_ERR_BADVERSION;
+ if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
+ return -FDT_ERR_BADVERSION;
+ } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
+ /* Unfinished sequential-write blob */
+ if (fdt_size_dt_struct(fdt) == 0)
+ return -FDT_ERR_BADSTATE;
+ } else {
+ return -FDT_ERR_BADMAGIC;
+ }
+
+ return 0;
+}
+
+const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
+{
+ const char *p;
+
+ if (fdt_version(fdt) >= 0x11)
+ if (((offset + len) < offset)
+ || ((offset + len) > fdt_size_dt_struct(fdt)))
+ return NULL;
+
+ p = _fdt_offset_ptr(fdt, offset);
+
+ if (p + len < p)
+ return NULL;
+ return p;
+}
+
+uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
+{
+ const uint32_t *tagp, *lenp;
+ uint32_t tag;
+ int offset = startoffset;
+ const char *p;
+
+ *nextoffset = -FDT_ERR_TRUNCATED;
+ tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
+ if (!tagp)
+ return FDT_END; /* premature end */
+ tag = fdt32_to_cpu(*tagp);
+ offset += FDT_TAGSIZE;
+
+ *nextoffset = -FDT_ERR_BADSTRUCTURE;
+ switch (tag) {
+ case FDT_BEGIN_NODE:
+ /* skip name */
+ do {
+ p = fdt_offset_ptr(fdt, offset++, 1);
+ } while (p && (*p != '\0'));
+ if (!p)
+ return FDT_END; /* premature end */
+ break;
+
+ case FDT_PROP:
+ lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
+ if (!lenp)
+ return FDT_END; /* premature end */
+ /* skip-name offset, length and value */
+ offset += sizeof(struct fdt_property) - FDT_TAGSIZE
+ + fdt32_to_cpu(*lenp);
+ break;
+
+ case FDT_END:
+ case FDT_END_NODE:
+ case FDT_NOP:
+ break;
+
+ default:
+ return FDT_END;
+ }
+
+ if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
+ return FDT_END; /* premature end */
+
+ *nextoffset = FDT_TAGALIGN(offset);
+ return tag;
+}
+
+int _fdt_check_node_offset(const void *fdt, int offset)
+{
+ if ((offset < 0) || (offset % FDT_TAGSIZE)
+ || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
+ return -FDT_ERR_BADOFFSET;
+
+ return offset;
+}
+
+int _fdt_check_prop_offset(const void *fdt, int offset)
+{
+ if ((offset < 0) || (offset % FDT_TAGSIZE)
+ || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP))
+ return -FDT_ERR_BADOFFSET;
+
+ return offset;
+}
+
+int fdt_next_node(const void *fdt, int offset, int *depth)
+{
+ int nextoffset = 0;
+ uint32_t tag;
+
+ if (offset >= 0)
+ if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
+ return nextoffset;
+
+ do {
+ offset = nextoffset;
+ tag = fdt_next_tag(fdt, offset, &nextoffset);
+
+ switch (tag) {
+ case FDT_PROP:
+ case FDT_NOP:
+ break;
+
+ case FDT_BEGIN_NODE:
+ if (depth)
+ (*depth)++;
+ break;
+
+ case FDT_END_NODE:
+ if (depth && ((--(*depth)) < 0))
+ return nextoffset;
+ break;
+
+ case FDT_END:
+ if ((nextoffset >= 0)
+ || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
+ return -FDT_ERR_NOTFOUND;
+ else
+ return nextoffset;
+ }
+ } while (tag != FDT_BEGIN_NODE);
+
+ return offset;
+}
+
+const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
+{
+ int len = strlen(s) + 1;
+ const char *last = strtab + tabsize - len;
+ const char *p;
+
+ for (p = strtab; p <= last; p++)
+ if (memcmp(p, s, len) == 0)
+ return p;
+ return NULL;
+}
+
+int fdt_move(const void *fdt, void *buf, int bufsize)
+{
+ FDT_CHECK_HEADER(fdt);
+
+ if (fdt_totalsize(fdt) > bufsize)
+ return -FDT_ERR_NOSPACE;
+
+ memmove(buf, fdt, fdt_totalsize(fdt));
+ return 0;
+}
diff -purN trunk/grub-core/lib/dtc/libfdt/fdt.h arm-uboot/grub-core/lib/dtc/libfdt/fdt.h
--- trunk/grub-core/lib/dtc/libfdt/fdt.h 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/fdt.h 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,60 @@
+#ifndef _FDT_H
+#define _FDT_H
+
+#ifndef __ASSEMBLY__
+
+struct fdt_header {
+ uint32_t magic; /* magic word FDT_MAGIC */
+ uint32_t totalsize; /* total size of DT block */
+ uint32_t off_dt_struct; /* offset to structure */
+ uint32_t off_dt_strings; /* offset to strings */
+ uint32_t off_mem_rsvmap; /* offset to memory reserve map */
+ uint32_t version; /* format version */
+ uint32_t last_comp_version; /* last compatible version */
+
+ /* version 2 fields below */
+ uint32_t boot_cpuid_phys; /* Which physical CPU id we're
+ booting on */
+ /* version 3 fields below */
+ uint32_t size_dt_strings; /* size of the strings block */
+
+ /* version 17 fields below */
+ uint32_t size_dt_struct; /* size of the structure block */
+};
+
+struct fdt_reserve_entry {
+ uint64_t address;
+ uint64_t size;
+};
+
+struct fdt_node_header {
+ uint32_t tag;
+ char name[0];
+};
+
+struct fdt_property {
+ uint32_t tag;
+ uint32_t len;
+ uint32_t nameoff;
+ char data[0];
+};
+
+#endif /* !__ASSEMBLY */
+
+#define FDT_MAGIC 0xd00dfeed /* 4: version, 4: total size */
+#define FDT_TAGSIZE sizeof(uint32_t)
+
+#define FDT_BEGIN_NODE 0x1 /* Start node: full name */
+#define FDT_END_NODE 0x2 /* End node */
+#define FDT_PROP 0x3 /* Property: name off,
+ size, content */
+#define FDT_NOP 0x4 /* nop */
+#define FDT_END 0x9
+
+#define FDT_V1_SIZE (7*sizeof(uint32_t))
+#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(uint32_t))
+#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(uint32_t))
+#define FDT_V16_SIZE FDT_V3_SIZE
+#define FDT_V17_SIZE (FDT_V16_SIZE + sizeof(uint32_t))
+
+#endif /* _FDT_H */
diff -purN trunk/grub-core/lib/dtc/libfdt/fdt_ro.c arm-uboot/grub-core/lib/dtc/libfdt/fdt_ro.c
--- trunk/grub-core/lib/dtc/libfdt/fdt_ro.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/fdt_ro.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,574 @@
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library 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.
+ *
+ * This library 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 library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+static int _fdt_nodename_eq(const void *fdt, int offset,
+ const char *s, int len)
+{
+ const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
+
+ if (! p)
+ /* short match */
+ return 0;
+
+ if (memcmp(p, s, len) != 0)
+ return 0;
+
+ if (p[len] == '\0')
+ return 1;
+ else if (!memchr(s, '@', len) && (p[len] == '@'))
+ return 1;
+ else
+ return 0;
+}
+
+const char *fdt_string(const void *fdt, int stroffset)
+{
+ return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
+}
+
+static int _fdt_string_eq(const void *fdt, int stroffset,
+ const char *s, int len)
+{
+ const char *p = fdt_string(fdt, stroffset);
+
+ return (strlen(p) == len) && (memcmp(p, s, len) == 0);
+}
+
+int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
+{
+ FDT_CHECK_HEADER(fdt);
+ *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
+ *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
+ return 0;
+}
+
+int fdt_num_mem_rsv(const void *fdt)
+{
+ int i = 0;
+
+ while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
+ i++;
+ return i;
+}
+
+static int _nextprop(const void *fdt, int offset)
+{
+ uint32_t tag;
+ int nextoffset;
+
+ do {
+ tag = fdt_next_tag(fdt, offset, &nextoffset);
+
+ switch (tag) {
+ case FDT_END:
+ if (nextoffset >= 0)
+ return -FDT_ERR_BADSTRUCTURE;
+ else
+ return nextoffset;
+
+ case FDT_PROP:
+ return offset;
+ }
+ offset = nextoffset;
+ } while (tag == FDT_NOP);
+
+ return -FDT_ERR_NOTFOUND;
+}
+
+int fdt_subnode_offset_namelen(const void *fdt, int offset,
+ const char *name, int namelen)
+{
+ int depth;
+
+ FDT_CHECK_HEADER(fdt);
+
+ for (depth = 0;
+ (offset >= 0) && (depth >= 0);
+ offset = fdt_next_node(fdt, offset, &depth))
+ if ((depth == 1)
+ && _fdt_nodename_eq(fdt, offset, name, namelen))
+ return offset;
+
+ if (depth < 0)
+ return -FDT_ERR_NOTFOUND;
+ return offset; /* error */
+}
+
+int fdt_subnode_offset(const void *fdt, int parentoffset,
+ const char *name)
+{
+ return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
+}
+
+int fdt_path_offset(const void *fdt, const char *path)
+{
+ const char *end = path + strlen(path);
+ const char *p = path;
+ int offset = 0;
+
+ FDT_CHECK_HEADER(fdt);
+
+ /* see if we have an alias */
+ if (*path != '/') {
+ const char *q = strchr(path, '/');
+
+ if (!q)
+ q = end;
+
+ p = fdt_get_alias_namelen(fdt, p, q - p);
+ if (!p)
+ return -FDT_ERR_BADPATH;
+ offset = fdt_path_offset(fdt, p);
+
+ p = q;
+ }
+
+ while (*p) {
+ const char *q;
+
+ while (*p == '/')
+ p++;
+ if (! *p)
+ return offset;
+ q = strchr(p, '/');
+ if (! q)
+ q = end;
+
+ offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
+ if (offset < 0)
+ return offset;
+
+ p = q;
+ }
+
+ return offset;
+}
+
+const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
+{
+ const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
+ int err;
+
+ if (((err = fdt_check_header(fdt)) != 0)
+ || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
+ goto fail;
+
+ if (len)
+ *len = strlen(nh->name);
+
+ return nh->name;
+
+ fail:
+ if (len)
+ *len = err;
+ return NULL;
+}
+
+int fdt_first_property_offset(const void *fdt, int nodeoffset)
+{
+ int offset;
+
+ if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
+ return offset;
+
+ return _nextprop(fdt, offset);
+}
+
+int fdt_next_property_offset(const void *fdt, int offset)
+{
+ if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0)
+ return offset;
+
+ return _nextprop(fdt, offset);
+}
+
+const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
+ int offset,
+ int *lenp)
+{
+ int err;
+ const struct fdt_property *prop;
+
+ if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) {
+ if (lenp)
+ *lenp = err;
+ return NULL;
+ }
+
+ prop = _fdt_offset_ptr(fdt, offset);
+
+ if (lenp)
+ *lenp = fdt32_to_cpu(prop->len);
+
+ return prop;
+}
+
+const struct fdt_property *fdt_get_property_namelen(const void *fdt,
+ int offset,
+ const char *name,
+ int namelen, int *lenp)
+{
+ for (offset = fdt_first_property_offset(fdt, offset);
+ (offset >= 0);
+ (offset = fdt_next_property_offset(fdt, offset))) {
+ const struct fdt_property *prop;
+
+ if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
+ offset = -FDT_ERR_INTERNAL;
+ break;
+ }
+ if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
+ name, namelen))
+ return prop;
+ }
+
+ if (lenp)
+ *lenp = offset;
+ return NULL;
+}
+
+const struct fdt_property *fdt_get_property(const void *fdt,
+ int nodeoffset,
+ const char *name, int *lenp)
+{
+ return fdt_get_property_namelen(fdt, nodeoffset, name,
+ strlen(name), lenp);
+}
+
+const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
+ const char *name, int namelen, int *lenp)
+{
+ const struct fdt_property *prop;
+
+ prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
+ if (! prop)
+ return NULL;
+
+ return prop->data;
+}
+
+const void *fdt_getprop_by_offset(const void *fdt, int offset,
+ const char **namep, int *lenp)
+{
+ const struct fdt_property *prop;
+
+ prop = fdt_get_property_by_offset(fdt, offset, lenp);
+ if (!prop)
+ return NULL;
+ if (namep)
+ *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
+ return prop->data;
+}
+
+const void *fdt_getprop(const void *fdt, int nodeoffset,
+ const char *name, int *lenp)
+{
+ return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
+}
+
+uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
+{
+ const uint32_t *php;
+ int len;
+
+ /* FIXME: This is a bit sub-optimal, since we potentially scan
+ * over all the properties twice. */
+ php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
+ if (!php || (len != sizeof(*php))) {
+ php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
+ if (!php || (len != sizeof(*php)))
+ return 0;
+ }
+
+ return fdt32_to_cpu(*php);
+}
+
+const char *fdt_get_alias_namelen(const void *fdt,
+ const char *name, int namelen)
+{
+ int aliasoffset;
+
+ aliasoffset = fdt_path_offset(fdt, "/aliases");
+ if (aliasoffset < 0)
+ return NULL;
+
+ return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
+}
+
+const char *fdt_get_alias(const void *fdt, const char *name)
+{
+ return fdt_get_alias_namelen(fdt, name, strlen(name));
+}
+
+int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
+{
+ int pdepth = 0, p = 0;
+ int offset, depth, namelen;
+ const char *name;
+
+ FDT_CHECK_HEADER(fdt);
+
+ if (buflen < 2)
+ return -FDT_ERR_NOSPACE;
+
+ for (offset = 0, depth = 0;
+ (offset >= 0) && (offset <= nodeoffset);
+ offset = fdt_next_node(fdt, offset, &depth)) {
+ while (pdepth > depth) {
+ do {
+ p--;
+ } while (buf[p-1] != '/');
+ pdepth--;
+ }
+
+ if (pdepth >= depth) {
+ name = fdt_get_name(fdt, offset, &namelen);
+ if (!name)
+ return namelen;
+ if ((p + namelen + 1) <= buflen) {
+ memcpy(buf + p, name, namelen);
+ p += namelen;
+ buf[p++] = '/';
+ pdepth++;
+ }
+ }
+
+ if (offset == nodeoffset) {
+ if (pdepth < (depth + 1))
+ return -FDT_ERR_NOSPACE;
+
+ if (p > 1) /* special case so that root path is "/", not "" */
+ p--;
+ buf[p] = '\0';
+ return 0;
+ }
+ }
+
+ if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
+ return -FDT_ERR_BADOFFSET;
+ else if (offset == -FDT_ERR_BADOFFSET)
+ return -FDT_ERR_BADSTRUCTURE;
+
+ return offset; /* error from fdt_next_node() */
+}
+
+int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
+ int supernodedepth, int *nodedepth)
+{
+ int offset, depth;
+ int supernodeoffset = -FDT_ERR_INTERNAL;
+
+ FDT_CHECK_HEADER(fdt);
+
+ if (supernodedepth < 0)
+ return -FDT_ERR_NOTFOUND;
+
+ for (offset = 0, depth = 0;
+ (offset >= 0) && (offset <= nodeoffset);
+ offset = fdt_next_node(fdt, offset, &depth)) {
+ if (depth == supernodedepth)
+ supernodeoffset = offset;
+
+ if (offset == nodeoffset) {
+ if (nodedepth)
+ *nodedepth = depth;
+
+ if (supernodedepth > depth)
+ return -FDT_ERR_NOTFOUND;
+ else
+ return supernodeoffset;
+ }
+ }
+
+ if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
+ return -FDT_ERR_BADOFFSET;
+ else if (offset == -FDT_ERR_BADOFFSET)
+ return -FDT_ERR_BADSTRUCTURE;
+
+ return offset; /* error from fdt_next_node() */
+}
+
+int fdt_node_depth(const void *fdt, int nodeoffset)
+{
+ int nodedepth;
+ int err;
+
+ err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
+ if (err)
+ return (err < 0) ? err : -FDT_ERR_INTERNAL;
+ return nodedepth;
+}
+
+int fdt_parent_offset(const void *fdt, int nodeoffset)
+{
+ int nodedepth = fdt_node_depth(fdt, nodeoffset);
+
+ if (nodedepth < 0)
+ return nodedepth;
+ return fdt_supernode_atdepth_offset(fdt, nodeoffset,
+ nodedepth - 1, NULL);
+}
+
+int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
+ const char *propname,
+ const void *propval, int proplen)
+{
+ int offset;
+ const void *val;
+ int len;
+
+ FDT_CHECK_HEADER(fdt);
+
+ /* FIXME: The algorithm here is pretty horrible: we scan each
+ * property of a node in fdt_getprop(), then if that didn't
+ * find what we want, we scan over them again making our way
+ * to the next node. Still it's the easiest to implement
+ * approach; performance can come later. */
+ for (offset = fdt_next_node(fdt, startoffset, NULL);
+ offset >= 0;
+ offset = fdt_next_node(fdt, offset, NULL)) {
+ val = fdt_getprop(fdt, offset, propname, &len);
+ if (val && (len == proplen)
+ && (memcmp(val, propval, len) == 0))
+ return offset;
+ }
+
+ return offset; /* error from fdt_next_node() */
+}
+
+int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
+{
+ int offset;
+
+ if ((phandle == 0) || (phandle == -1))
+ return -FDT_ERR_BADPHANDLE;
+
+ FDT_CHECK_HEADER(fdt);
+
+ /* FIXME: The algorithm here is pretty horrible: we
+ * potentially scan each property of a node in
+ * fdt_get_phandle(), then if that didn't find what
+ * we want, we scan over them again making our way to the next
+ * node. Still it's the easiest to implement approach;
+ * performance can come later. */
+ for (offset = fdt_next_node(fdt, -1, NULL);
+ offset >= 0;
+ offset = fdt_next_node(fdt, offset, NULL)) {
+ if (fdt_get_phandle(fdt, offset) == phandle)
+ return offset;
+ }
+
+ return offset; /* error from fdt_next_node() */
+}
+
+static int _fdt_stringlist_contains(const char *strlist, int listlen,
+ const char *str)
+{
+ int len = strlen(str);
+ const char *p;
+
+ while (listlen >= len) {
+ if (memcmp(str, strlist, len+1) == 0)
+ return 1;
+ p = memchr(strlist, '\0', listlen);
+ if (!p)
+ return 0; /* malformed strlist.. */
+ listlen -= (p-strlist) + 1;
+ strlist = p + 1;
+ }
+ return 0;
+}
+
+int fdt_node_check_compatible(const void *fdt, int nodeoffset,
+ const char *compatible)
+{
+ const void *prop;
+ int len;
+
+ prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
+ if (!prop)
+ return len;
+ if (_fdt_stringlist_contains(prop, len, compatible))
+ return 0;
+ else
+ return 1;
+}
+
+int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
+ const char *compatible)
+{
+ int offset, err;
+
+ FDT_CHECK_HEADER(fdt);
+
+ /* FIXME: The algorithm here is pretty horrible: we scan each
+ * property of a node in fdt_node_check_compatible(), then if
+ * that didn't find what we want, we scan over them again
+ * making our way to the next node. Still it's the easiest to
+ * implement approach; performance can come later. */
+ for (offset = fdt_next_node(fdt, startoffset, NULL);
+ offset >= 0;
+ offset = fdt_next_node(fdt, offset, NULL)) {
+ err = fdt_node_check_compatible(fdt, offset, compatible);
+ if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
+ return err;
+ else if (err == 0)
+ return offset;
+ }
+
+ return offset; /* error from fdt_next_node() */
+}
diff -purN trunk/grub-core/lib/dtc/libfdt/fdt_rw.c arm-uboot/grub-core/lib/dtc/libfdt/fdt_rw.c
--- trunk/grub-core/lib/dtc/libfdt/fdt_rw.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/fdt_rw.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,465 @@
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library 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.
+ *
+ * This library 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 library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+static int _fdt_blocks_misordered(const void *fdt,
+ int mem_rsv_size, int struct_size)
+{
+ return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
+ || (fdt_off_dt_struct(fdt) <
+ (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
+ || (fdt_off_dt_strings(fdt) <
+ (fdt_off_dt_struct(fdt) + struct_size))
+ || (fdt_totalsize(fdt) <
+ (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
+}
+
+static int _fdt_rw_check_header(void *fdt)
+{
+ FDT_CHECK_HEADER(fdt);
+
+ if (fdt_version(fdt) < 17)
+ return -FDT_ERR_BADVERSION;
+ if (_fdt_blocks_misordered(fdt, sizeof(struct fdt_reserve_entry),
+ fdt_size_dt_struct(fdt)))
+ return -FDT_ERR_BADLAYOUT;
+ if (fdt_version(fdt) > 17)
+ fdt_set_version(fdt, 17);
+
+ return 0;
+}
+
+#define FDT_RW_CHECK_HEADER(fdt) \
+ { \
+ int err; \
+ if ((err = _fdt_rw_check_header(fdt)) != 0) \
+ return err; \
+ }
+
+static inline int _fdt_data_size(void *fdt)
+{
+ return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
+}
+
+static int _fdt_splice(void *fdt, void *splicepoint, int oldlen, int newlen)
+{
+ char *p = splicepoint;
+ char *end = (char *)fdt + _fdt_data_size(fdt);
+
+ if (((p + oldlen) < p) || ((p + oldlen) > end))
+ return -FDT_ERR_BADOFFSET;
+ if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt)))
+ return -FDT_ERR_NOSPACE;
+ memmove(p + newlen, p + oldlen, end - p - oldlen);
+ return 0;
+}
+
+static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p,
+ int oldn, int newn)
+{
+ int delta = (newn - oldn) * sizeof(*p);
+ int err;
+ err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
+ if (err)
+ return err;
+ fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
+ fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
+ return 0;
+}
+
+static int _fdt_splice_struct(void *fdt, void *p,
+ int oldlen, int newlen)
+{
+ int delta = newlen - oldlen;
+ int err;
+
+ if ((err = _fdt_splice(fdt, p, oldlen, newlen)))
+ return err;
+
+ fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
+ fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
+ return 0;
+}
+
+static int _fdt_splice_string(void *fdt, int newlen)
+{
+ void *p = (char *)fdt
+ + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
+ int err;
+
+ if ((err = _fdt_splice(fdt, p, 0, newlen)))
+ return err;
+
+ fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
+ return 0;
+}
+
+static int _fdt_find_add_string(void *fdt, const char *s)
+{
+ char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
+ const char *p;
+ char *new;
+ int len = strlen(s) + 1;
+ int err;
+
+ p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
+ if (p)
+ /* found it */
+ return (p - strtab);
+
+ new = strtab + fdt_size_dt_strings(fdt);
+ err = _fdt_splice_string(fdt, len);
+ if (err)
+ return err;
+
+ memcpy(new, s, len);
+ return (new - strtab);
+}
+
+int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
+{
+ struct fdt_reserve_entry *re;
+ int err;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt));
+ err = _fdt_splice_mem_rsv(fdt, re, 0, 1);
+ if (err)
+ return err;
+
+ re->address = cpu_to_fdt64(address);
+ re->size = cpu_to_fdt64(size);
+ return 0;
+}
+
+int fdt_del_mem_rsv(void *fdt, int n)
+{
+ struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n);
+ int err;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ if (n >= fdt_num_mem_rsv(fdt))
+ return -FDT_ERR_NOTFOUND;
+
+ err = _fdt_splice_mem_rsv(fdt, re, 1, 0);
+ if (err)
+ return err;
+ return 0;
+}
+
+static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name,
+ int len, struct fdt_property **prop)
+{
+ int oldlen;
+ int err;
+
+ *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
+ if (! (*prop))
+ return oldlen;
+
+ if ((err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
+ FDT_TAGALIGN(len))))
+ return err;
+
+ (*prop)->len = cpu_to_fdt32(len);
+ return 0;
+}
+
+static int _fdt_add_property(void *fdt, int nodeoffset, const char *name,
+ int len, struct fdt_property **prop)
+{
+ int proplen;
+ int nextoffset;
+ int namestroff;
+ int err;
+
+ if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
+ return nextoffset;
+
+ namestroff = _fdt_find_add_string(fdt, name);
+ if (namestroff < 0)
+ return namestroff;
+
+ *prop = _fdt_offset_ptr_w(fdt, nextoffset);
+ proplen = sizeof(**prop) + FDT_TAGALIGN(len);
+
+ err = _fdt_splice_struct(fdt, *prop, 0, proplen);
+ if (err)
+ return err;
+
+ (*prop)->tag = cpu_to_fdt32(FDT_PROP);
+ (*prop)->nameoff = cpu_to_fdt32(namestroff);
+ (*prop)->len = cpu_to_fdt32(len);
+ return 0;
+}
+
+int fdt_set_name(void *fdt, int nodeoffset, const char *name)
+{
+ char *namep;
+ int oldlen, newlen;
+ int err;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
+ if (!namep)
+ return oldlen;
+
+ newlen = strlen(name);
+
+ err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1),
+ FDT_TAGALIGN(newlen+1));
+ if (err)
+ return err;
+
+ memcpy(namep, name, newlen+1);
+ return 0;
+}
+
+int fdt_setprop(void *fdt, int nodeoffset, const char *name,
+ const void *val, int len)
+{
+ struct fdt_property *prop;
+ int err;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop);
+ if (err == -FDT_ERR_NOTFOUND)
+ err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
+ if (err)
+ return err;
+
+ memcpy(prop->data, val, len);
+ return 0;
+}
+
+int fdt_delprop(void *fdt, int nodeoffset, const char *name)
+{
+ struct fdt_property *prop;
+ int len, proplen;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
+ if (! prop)
+ return len;
+
+ proplen = sizeof(*prop) + FDT_TAGALIGN(len);
+ return _fdt_splice_struct(fdt, prop, proplen, 0);
+}
+
+int fdt_add_subnode_namelen(void *fdt, int parentoffset,
+ const char *name, int namelen)
+{
+ struct fdt_node_header *nh;
+ int offset, nextoffset;
+ int nodelen;
+ int err;
+ uint32_t tag;
+ uint32_t *endtag;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
+ if (offset >= 0)
+ return -FDT_ERR_EXISTS;
+ else if (offset != -FDT_ERR_NOTFOUND)
+ return offset;
+
+ /* Try to place the new node after the parent's properties */
+ fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
+ do {
+ offset = nextoffset;
+ tag = fdt_next_tag(fdt, offset, &nextoffset);
+ } while ((tag == FDT_PROP) || (tag == FDT_NOP));
+
+ nh = _fdt_offset_ptr_w(fdt, offset);
+ nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
+
+ err = _fdt_splice_struct(fdt, nh, 0, nodelen);
+ if (err)
+ return err;
+
+ nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
+ memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
+ memcpy(nh->name, name, namelen);
+ endtag = (uint32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
+ *endtag = cpu_to_fdt32(FDT_END_NODE);
+
+ return offset;
+}
+
+int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
+{
+ return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
+}
+
+int fdt_del_node(void *fdt, int nodeoffset)
+{
+ int endoffset;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ endoffset = _fdt_node_end_offset(fdt, nodeoffset);
+ if (endoffset < 0)
+ return endoffset;
+
+ return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
+ endoffset - nodeoffset, 0);
+}
+
+static void _fdt_packblocks(const char *old, char *new,
+ int mem_rsv_size, int struct_size)
+{
+ int mem_rsv_off, struct_off, strings_off;
+
+ mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
+ struct_off = mem_rsv_off + mem_rsv_size;
+ strings_off = struct_off + struct_size;
+
+ memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
+ fdt_set_off_mem_rsvmap(new, mem_rsv_off);
+
+ memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
+ fdt_set_off_dt_struct(new, struct_off);
+ fdt_set_size_dt_struct(new, struct_size);
+
+ memmove(new + strings_off, old + fdt_off_dt_strings(old),
+ fdt_size_dt_strings(old));
+ fdt_set_off_dt_strings(new, strings_off);
+ fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
+}
+
+int fdt_open_into(const void *fdt, void *buf, int bufsize)
+{
+ int err;
+ int mem_rsv_size, struct_size;
+ int newsize;
+ const char *fdtstart = fdt;
+ const char *fdtend = fdtstart + fdt_totalsize(fdt);
+ char *tmp;
+
+ FDT_CHECK_HEADER(fdt);
+
+ mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
+ * sizeof(struct fdt_reserve_entry);
+
+ if (fdt_version(fdt) >= 17) {
+ struct_size = fdt_size_dt_struct(fdt);
+ } else {
+ struct_size = 0;
+ while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
+ ;
+ if (struct_size < 0)
+ return struct_size;
+ }
+
+ if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) {
+ /* no further work necessary */
+ err = fdt_move(fdt, buf, bufsize);
+ if (err)
+ return err;
+ fdt_set_version(buf, 17);
+ fdt_set_size_dt_struct(buf, struct_size);
+ fdt_set_totalsize(buf, bufsize);
+ return 0;
+ }
+
+ /* Need to reorder */
+ newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
+ + struct_size + fdt_size_dt_strings(fdt);
+
+ if (bufsize < newsize)
+ return -FDT_ERR_NOSPACE;
+
+ /* First attempt to build converted tree at beginning of buffer */
+ tmp = buf;
+ /* But if that overlaps with the old tree... */
+ if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
+ /* Try right after the old tree instead */
+ tmp = (char *)(uintptr_t)fdtend;
+ if ((tmp + newsize) > ((char *)buf + bufsize))
+ return -FDT_ERR_NOSPACE;
+ }
+
+ _fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size);
+ memmove(buf, tmp, newsize);
+
+ fdt_set_magic(buf, FDT_MAGIC);
+ fdt_set_totalsize(buf, bufsize);
+ fdt_set_version(buf, 17);
+ fdt_set_last_comp_version(buf, 16);
+ fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
+
+ return 0;
+}
+
+int fdt_pack(void *fdt)
+{
+ int mem_rsv_size;
+
+ FDT_RW_CHECK_HEADER(fdt);
+
+ mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
+ * sizeof(struct fdt_reserve_entry);
+ _fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
+ fdt_set_totalsize(fdt, _fdt_data_size(fdt));
+
+ return 0;
+}
diff -purN trunk/grub-core/lib/dtc/libfdt/fdt_strerror.c arm-uboot/grub-core/lib/dtc/libfdt/fdt_strerror.c
--- trunk/grub-core/lib/dtc/libfdt/fdt_strerror.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/fdt_strerror.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,96 @@
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library 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.
+ *
+ * This library 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 library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+struct fdt_errtabent {
+ const char *str;
+};
+
+#define FDT_ERRTABENT(val) \
+ [(val)] = { .str = #val, }
+
+static struct fdt_errtabent fdt_errtable[] = {
+ FDT_ERRTABENT(FDT_ERR_NOTFOUND),
+ FDT_ERRTABENT(FDT_ERR_EXISTS),
+ FDT_ERRTABENT(FDT_ERR_NOSPACE),
+
+ FDT_ERRTABENT(FDT_ERR_BADOFFSET),
+ FDT_ERRTABENT(FDT_ERR_BADPATH),
+ FDT_ERRTABENT(FDT_ERR_BADSTATE),
+
+ FDT_ERRTABENT(FDT_ERR_TRUNCATED),
+ FDT_ERRTABENT(FDT_ERR_BADMAGIC),
+ FDT_ERRTABENT(FDT_ERR_BADVERSION),
+ FDT_ERRTABENT(FDT_ERR_BADSTRUCTURE),
+ FDT_ERRTABENT(FDT_ERR_BADLAYOUT),
+};
+#define FDT_ERRTABSIZE (sizeof(fdt_errtable) / sizeof(fdt_errtable[0]))
+
+const char *fdt_strerror(int errval)
+{
+ if (errval > 0)
+ return "<valid offset/length>";
+ else if (errval == 0)
+ return "<no error>";
+ else if (errval > -FDT_ERRTABSIZE) {
+ const char *s = fdt_errtable[-errval].str;
+
+ if (s)
+ return s;
+ }
+
+ return "<unknown error>";
+}
diff -purN trunk/grub-core/lib/dtc/libfdt/fdt_sw.c arm-uboot/grub-core/lib/dtc/libfdt/fdt_sw.c
--- trunk/grub-core/lib/dtc/libfdt/fdt_sw.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/fdt_sw.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,256 @@
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library 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.
+ *
+ * This library 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 library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+static int _fdt_sw_check_header(void *fdt)
+{
+ if (fdt_magic(fdt) != FDT_SW_MAGIC)
+ return -FDT_ERR_BADMAGIC;
+ /* FIXME: should check more details about the header state */
+ return 0;
+}
+
+#define FDT_SW_CHECK_HEADER(fdt) \
+ { \
+ int err; \
+ if ((err = _fdt_sw_check_header(fdt)) != 0) \
+ return err; \
+ }
+
+static void *_fdt_grab_space(void *fdt, size_t len)
+{
+ int offset = fdt_size_dt_struct(fdt);
+ int spaceleft;
+
+ spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
+ - fdt_size_dt_strings(fdt);
+
+ if ((offset + len < offset) || (offset + len > spaceleft))
+ return NULL;
+
+ fdt_set_size_dt_struct(fdt, offset + len);
+ return _fdt_offset_ptr_w(fdt, offset);
+}
+
+int fdt_create(void *buf, int bufsize)
+{
+ void *fdt = buf;
+
+ if (bufsize < sizeof(struct fdt_header))
+ return -FDT_ERR_NOSPACE;
+
+ memset(buf, 0, bufsize);
+
+ fdt_set_magic(fdt, FDT_SW_MAGIC);
+ fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
+ fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
+ fdt_set_totalsize(fdt, bufsize);
+
+ fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header),
+ sizeof(struct fdt_reserve_entry)));
+ fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
+ fdt_set_off_dt_strings(fdt, bufsize);
+
+ return 0;
+}
+
+int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
+{
+ struct fdt_reserve_entry *re;
+ int offset;
+
+ FDT_SW_CHECK_HEADER(fdt);
+
+ if (fdt_size_dt_struct(fdt))
+ return -FDT_ERR_BADSTATE;
+
+ offset = fdt_off_dt_struct(fdt);
+ if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
+ return -FDT_ERR_NOSPACE;
+
+ re = (struct fdt_reserve_entry *)((char *)fdt + offset);
+ re->address = cpu_to_fdt64(addr);
+ re->size = cpu_to_fdt64(size);
+
+ fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
+
+ return 0;
+}
+
+int fdt_finish_reservemap(void *fdt)
+{
+ return fdt_add_reservemap_entry(fdt, 0, 0);
+}
+
+int fdt_begin_node(void *fdt, const char *name)
+{
+ struct fdt_node_header *nh;
+ int namelen = strlen(name) + 1;
+
+ FDT_SW_CHECK_HEADER(fdt);
+
+ nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
+ if (! nh)
+ return -FDT_ERR_NOSPACE;
+
+ nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
+ memcpy(nh->name, name, namelen);
+ return 0;
+}
+
+int fdt_end_node(void *fdt)
+{
+ uint32_t *en;
+
+ FDT_SW_CHECK_HEADER(fdt);
+
+ en = _fdt_grab_space(fdt, FDT_TAGSIZE);
+ if (! en)
+ return -FDT_ERR_NOSPACE;
+
+ *en = cpu_to_fdt32(FDT_END_NODE);
+ return 0;
+}
+
+static int _fdt_find_add_string(void *fdt, const char *s)
+{
+ char *strtab = (char *)fdt + fdt_totalsize(fdt);
+ const char *p;
+ int strtabsize = fdt_size_dt_strings(fdt);
+ int len = strlen(s) + 1;
+ int struct_top, offset;
+
+ p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
+ if (p)
+ return p - strtab;
+
+ /* Add it */
+ offset = -strtabsize - len;
+ struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
+ if (fdt_totalsize(fdt) + offset < struct_top)
+ return 0; /* no more room :( */
+
+ memcpy(strtab + offset, s, len);
+ fdt_set_size_dt_strings(fdt, strtabsize + len);
+ return offset;
+}
+
+int fdt_property(void *fdt, const char *name, const void *val, int len)
+{
+ struct fdt_property *prop;
+ int nameoff;
+
+ FDT_SW_CHECK_HEADER(fdt);
+
+ nameoff = _fdt_find_add_string(fdt, name);
+ if (nameoff == 0)
+ return -FDT_ERR_NOSPACE;
+
+ prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
+ if (! prop)
+ return -FDT_ERR_NOSPACE;
+
+ prop->tag = cpu_to_fdt32(FDT_PROP);
+ prop->nameoff = cpu_to_fdt32(nameoff);
+ prop->len = cpu_to_fdt32(len);
+ memcpy(prop->data, val, len);
+ return 0;
+}
+
+int fdt_finish(void *fdt)
+{
+ char *p = (char *)fdt;
+ uint32_t *end;
+ int oldstroffset, newstroffset;
+ uint32_t tag;
+ int offset, nextoffset;
+
+ FDT_SW_CHECK_HEADER(fdt);
+
+ /* Add terminator */
+ end = _fdt_grab_space(fdt, sizeof(*end));
+ if (! end)
+ return -FDT_ERR_NOSPACE;
+ *end = cpu_to_fdt32(FDT_END);
+
+ /* Relocate the string table */
+ oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
+ newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
+ memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
+ fdt_set_off_dt_strings(fdt, newstroffset);
+
+ /* Walk the structure, correcting string offsets */
+ offset = 0;
+ while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
+ if (tag == FDT_PROP) {
+ struct fdt_property *prop =
+ _fdt_offset_ptr_w(fdt, offset);
+ int nameoff;
+
+ nameoff = fdt32_to_cpu(prop->nameoff);
+ nameoff += fdt_size_dt_strings(fdt);
+ prop->nameoff = cpu_to_fdt32(nameoff);
+ }
+ offset = nextoffset;
+ }
+ if (nextoffset < 0)
+ return nextoffset;
+
+ /* Finally, adjust the header */
+ fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
+ fdt_set_magic(fdt, FDT_MAGIC);
+ return 0;
+}
diff -purN trunk/grub-core/lib/dtc/libfdt/fdt_wip.c arm-uboot/grub-core/lib/dtc/libfdt/fdt_wip.c
--- trunk/grub-core/lib/dtc/libfdt/fdt_wip.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/fdt_wip.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,118 @@
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library 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.
+ *
+ * This library 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 library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "libfdt_env.h"
+
+#include <fdt.h>
+#include <libfdt.h>
+
+#include "libfdt_internal.h"
+
+int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
+ const void *val, int len)
+{
+ void *propval;
+ int proplen;
+
+ propval = fdt_getprop_w(fdt, nodeoffset, name, &proplen);
+ if (! propval)
+ return proplen;
+
+ if (proplen != len)
+ return -FDT_ERR_NOSPACE;
+
+ memcpy(propval, val, len);
+ return 0;
+}
+
+static void _fdt_nop_region(void *start, int len)
+{
+ uint32_t *p;
+
+ for (p = start; (char *)p < ((char *)start + len); p++)
+ *p = cpu_to_fdt32(FDT_NOP);
+}
+
+int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
+{
+ struct fdt_property *prop;
+ int len;
+
+ prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
+ if (! prop)
+ return len;
+
+ _fdt_nop_region(prop, len + sizeof(*prop));
+
+ return 0;
+}
+
+int _fdt_node_end_offset(void *fdt, int offset)
+{
+ int depth = 0;
+
+ while ((offset >= 0) && (depth >= 0))
+ offset = fdt_next_node(fdt, offset, &depth);
+
+ return offset;
+}
+
+int fdt_nop_node(void *fdt, int nodeoffset)
+{
+ int endoffset;
+
+ endoffset = _fdt_node_end_offset(fdt, nodeoffset);
+ if (endoffset < 0)
+ return endoffset;
+
+ _fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0),
+ endoffset - nodeoffset);
+ return 0;
+}
diff -purN trunk/grub-core/lib/dtc/libfdt/libfdt_env.h arm-uboot/grub-core/lib/dtc/libfdt/libfdt_env.h
--- trunk/grub-core/lib/dtc/libfdt/libfdt_env.h 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/libfdt_env.h 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,23 @@
+#ifndef _LIBFDT_ENV_H
+#define _LIBFDT_ENV_H
+
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+#define _B(n) ((unsigned long long)((uint8_t *)&x)[n])
+static inline uint32_t fdt32_to_cpu(uint32_t x)
+{
+ return (_B(0) << 24) | (_B(1) << 16) | (_B(2) << 8) | _B(3);
+}
+#define cpu_to_fdt32(x) fdt32_to_cpu(x)
+
+static inline uint64_t fdt64_to_cpu(uint64_t x)
+{
+ return (_B(0) << 56) | (_B(1) << 48) | (_B(2) << 40) | (_B(3) << 32)
+ | (_B(4) << 24) | (_B(5) << 16) | (_B(6) << 8) | _B(7);
+}
+#define cpu_to_fdt64(x) fdt64_to_cpu(x)
+#undef _B
+
+#endif /* _LIBFDT_ENV_H */
diff -purN trunk/grub-core/lib/dtc/libfdt/libfdt.h arm-uboot/grub-core/lib/dtc/libfdt/libfdt.h
--- trunk/grub-core/lib/dtc/libfdt/libfdt.h 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/libfdt.h 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,1235 @@
+#ifndef _LIBFDT_H
+#define _LIBFDT_H
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library 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.
+ *
+ * This library 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 library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <libfdt_env.h>
+#include <fdt.h>
+
+#define FDT_FIRST_SUPPORTED_VERSION 0x10
+#define FDT_LAST_SUPPORTED_VERSION 0x11
+
+/* Error codes: informative error codes */
+#define FDT_ERR_NOTFOUND 1
+ /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
+#define FDT_ERR_EXISTS 2
+ /* FDT_ERR_EXISTS: Attemped to create a node or property which
+ * already exists */
+#define FDT_ERR_NOSPACE 3
+ /* FDT_ERR_NOSPACE: Operation needed to expand the device
+ * tree, but its buffer did not have sufficient space to
+ * contain the expanded tree. Use fdt_open_into() to move the
+ * device tree to a buffer with more space. */
+
+/* Error codes: codes for bad parameters */
+#define FDT_ERR_BADOFFSET 4
+ /* FDT_ERR_BADOFFSET: Function was passed a structure block
+ * offset which is out-of-bounds, or which points to an
+ * unsuitable part of the structure for the operation. */
+#define FDT_ERR_BADPATH 5
+ /* FDT_ERR_BADPATH: Function was passed a badly formatted path
+ * (e.g. missing a leading / for a function which requires an
+ * absolute path) */
+#define FDT_ERR_BADPHANDLE 6
+ /* FDT_ERR_BADPHANDLE: Function was passed an invalid phandle
+ * value. phandle values of 0 and -1 are not permitted. */
+#define FDT_ERR_BADSTATE 7
+ /* FDT_ERR_BADSTATE: Function was passed an incomplete device
+ * tree created by the sequential-write functions, which is
+ * not sufficiently complete for the requested operation. */
+
+/* Error codes: codes for bad device tree blobs */
+#define FDT_ERR_TRUNCATED 8
+ /* FDT_ERR_TRUNCATED: Structure block of the given device tree
+ * ends without an FDT_END tag. */
+#define FDT_ERR_BADMAGIC 9
+ /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
+ * device tree at all - it is missing the flattened device
+ * tree magic number. */
+#define FDT_ERR_BADVERSION 10
+ /* FDT_ERR_BADVERSION: Given device tree has a version which
+ * can't be handled by the requested operation. For
+ * read-write functions, this may mean that fdt_open_into() is
+ * required to convert the tree to the expected version. */
+#define FDT_ERR_BADSTRUCTURE 11
+ /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
+ * structure block or other serious error (e.g. misnested
+ * nodes, or subnodes preceding properties). */
+#define FDT_ERR_BADLAYOUT 12
+ /* FDT_ERR_BADLAYOUT: For read-write functions, the given
+ * device tree has it's sub-blocks in an order that the
+ * function can't handle (memory reserve map, then structure,
+ * then strings). Use fdt_open_into() to reorganize the tree
+ * into a form suitable for the read-write operations. */
+
+/* "Can't happen" error indicating a bug in libfdt */
+#define FDT_ERR_INTERNAL 13
+ /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
+ * Should never be returned, if it is, it indicates a bug in
+ * libfdt itself. */
+
+#define FDT_ERR_MAX 13
+
+/**********************************************************************/
+/* Low-level functions (you probably don't need these) */
+/**********************************************************************/
+
+const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
+static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
+{
+ return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
+}
+
+uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
+
+/**********************************************************************/
+/* Traversal functions */
+/**********************************************************************/
+
+int fdt_next_node(const void *fdt, int offset, int *depth);
+
+/**********************************************************************/
+/* General functions */
+/**********************************************************************/
+
+#define fdt_get_header(fdt, field) \
+ (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
+#define fdt_magic(fdt) (fdt_get_header(fdt, magic))
+#define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
+#define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
+#define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
+#define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
+#define fdt_version(fdt) (fdt_get_header(fdt, version))
+#define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
+#define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
+#define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
+#define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
+
+#define __fdt_set_hdr(name) \
+ static inline void fdt_set_##name(void *fdt, uint32_t val) \
+ { \
+ struct fdt_header *fdth = (struct fdt_header*)fdt; \
+ fdth->name = cpu_to_fdt32(val); \
+ }
+__fdt_set_hdr(magic);
+__fdt_set_hdr(totalsize);
+__fdt_set_hdr(off_dt_struct);
+__fdt_set_hdr(off_dt_strings);
+__fdt_set_hdr(off_mem_rsvmap);
+__fdt_set_hdr(version);
+__fdt_set_hdr(last_comp_version);
+__fdt_set_hdr(boot_cpuid_phys);
+__fdt_set_hdr(size_dt_strings);
+__fdt_set_hdr(size_dt_struct);
+#undef __fdt_set_hdr
+
+/**
+ * fdt_check_header - sanity check a device tree or possible device tree
+ * @fdt: pointer to data which might be a flattened device tree
+ *
+ * fdt_check_header() checks that the given buffer contains what
+ * appears to be a flattened device tree with sane information in its
+ * header.
+ *
+ * returns:
+ * 0, if the buffer appears to contain a valid device tree
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE, standard meanings, as above
+ */
+int fdt_check_header(const void *fdt);
+
+/**
+ * fdt_move - move a device tree around in memory
+ * @fdt: pointer to the device tree to move
+ * @buf: pointer to memory where the device is to be moved
+ * @bufsize: size of the memory space at buf
+ *
+ * fdt_move() relocates, if possible, the device tree blob located at
+ * fdt to the buffer at buf of size bufsize. The buffer may overlap
+ * with the existing device tree blob at fdt. Therefore,
+ * fdt_move(fdt, fdt, fdt_totalsize(fdt))
+ * should always succeed.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE, standard meanings
+ */
+int fdt_move(const void *fdt, void *buf, int bufsize);
+
+/**********************************************************************/
+/* Read-only functions */
+/**********************************************************************/
+
+/**
+ * fdt_string - retrieve a string from the strings block of a device tree
+ * @fdt: pointer to the device tree blob
+ * @stroffset: offset of the string within the strings block (native endian)
+ *
+ * fdt_string() retrieves a pointer to a single string from the
+ * strings block of the device tree blob at fdt.
+ *
+ * returns:
+ * a pointer to the string, on success
+ * NULL, if stroffset is out of bounds
+ */
+const char *fdt_string(const void *fdt, int stroffset);
+
+/**
+ * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
+ * @fdt: pointer to the device tree blob
+ *
+ * Returns the number of entries in the device tree blob's memory
+ * reservation map. This does not include the terminating 0,0 entry
+ * or any other (0,0) entries reserved for expansion.
+ *
+ * returns:
+ * the number of entries
+ */
+int fdt_num_mem_rsv(const void *fdt);
+
+/**
+ * fdt_get_mem_rsv - retrieve one memory reserve map entry
+ * @fdt: pointer to the device tree blob
+ * @address, @size: pointers to 64-bit variables
+ *
+ * On success, *address and *size will contain the address and size of
+ * the n-th reserve map entry from the device tree blob, in
+ * native-endian format.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE, standard meanings
+ */
+int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
+
+/**
+ * fdt_subnode_offset_namelen - find a subnode based on substring
+ * @fdt: pointer to the device tree blob
+ * @parentoffset: structure block offset of a node
+ * @name: name of the subnode to locate
+ * @namelen: number of characters of name to consider
+ *
+ * Identical to fdt_subnode_offset(), but only examine the first
+ * namelen characters of name for matching the subnode name. This is
+ * useful for finding subnodes based on a portion of a larger string,
+ * such as a full path.
+ */
+int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
+ const char *name, int namelen);
+/**
+ * fdt_subnode_offset - find a subnode of a given node
+ * @fdt: pointer to the device tree blob
+ * @parentoffset: structure block offset of a node
+ * @name: name of the subnode to locate
+ *
+ * fdt_subnode_offset() finds a subnode of the node at structure block
+ * offset parentoffset with the given name. name may include a unit
+ * address, in which case fdt_subnode_offset() will find the subnode
+ * with that unit address, or the unit address may be omitted, in
+ * which case fdt_subnode_offset() will find an arbitrary subnode
+ * whose name excluding unit address matches the given name.
+ *
+ * returns:
+ * structure block offset of the requested subnode (>=0), on success
+ * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
+ * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings.
+ */
+int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
+
+/**
+ * fdt_path_offset - find a tree node by its full path
+ * @fdt: pointer to the device tree blob
+ * @path: full path of the node to locate
+ *
+ * fdt_path_offset() finds a node of a given path in the device tree.
+ * Each path component may omit the unit address portion, but the
+ * results of this are undefined if any such path component is
+ * ambiguous (that is if there are multiple nodes at the relevant
+ * level matching the given component, differentiated only by unit
+ * address).
+ *
+ * returns:
+ * structure block offset of the node with the requested path (>=0), on success
+ * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
+ * -FDT_ERR_NOTFOUND, if the requested node does not exist
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings.
+ */
+int fdt_path_offset(const void *fdt, const char *path);
+
+/**
+ * fdt_get_name - retrieve the name of a given node
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: structure block offset of the starting node
+ * @lenp: pointer to an integer variable (will be overwritten) or NULL
+ *
+ * fdt_get_name() retrieves the name (including unit address) of the
+ * device tree node at structure block offset nodeoffset. If lenp is
+ * non-NULL, the length of this name is also returned, in the integer
+ * pointed to by lenp.
+ *
+ * returns:
+ * pointer to the node's name, on success
+ * If lenp is non-NULL, *lenp contains the length of that name (>=0)
+ * NULL, on error
+ * if lenp is non-NULL *lenp contains an error code (<0):
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE, standard meanings
+ */
+const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
+
+/**
+ * fdt_first_property_offset - find the offset of a node's first property
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: structure block offset of a node
+ *
+ * fdt_first_property_offset() finds the first property of the node at
+ * the given structure block offset.
+ *
+ * returns:
+ * structure block offset of the property (>=0), on success
+ * -FDT_ERR_NOTFOUND, if the requested node has no properties
+ * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings.
+ */
+int fdt_first_property_offset(const void *fdt, int nodeoffset);
+
+/**
+ * fdt_next_property_offset - step through a node's properties
+ * @fdt: pointer to the device tree blob
+ * @offset: structure block offset of a property
+ *
+ * fdt_next_property_offset() finds the property immediately after the
+ * one at the given structure block offset. This will be a property
+ * of the same node as the given property.
+ *
+ * returns:
+ * structure block offset of the next property (>=0), on success
+ * -FDT_ERR_NOTFOUND, if the given property is the last in its node
+ * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings.
+ */
+int fdt_next_property_offset(const void *fdt, int offset);
+
+/**
+ * fdt_get_property_by_offset - retrieve the property at a given offset
+ * @fdt: pointer to the device tree blob
+ * @offset: offset of the property to retrieve
+ * @lenp: pointer to an integer variable (will be overwritten) or NULL
+ *
+ * fdt_get_property_by_offset() retrieves a pointer to the
+ * fdt_property structure within the device tree blob at the given
+ * offset. If lenp is non-NULL, the length of the property value is
+ * also returned, in the integer pointed to by lenp.
+ *
+ * returns:
+ * pointer to the structure representing the property
+ * if lenp is non-NULL, *lenp contains the length of the property
+ * value (>=0)
+ * NULL, on error
+ * if lenp is non-NULL, *lenp contains an error code (<0):
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
+ int offset,
+ int *lenp);
+
+/**
+ * fdt_get_property_namelen - find a property based on substring
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose property to find
+ * @name: name of the property to find
+ * @namelen: number of characters of name to consider
+ * @lenp: pointer to an integer variable (will be overwritten) or NULL
+ *
+ * Identical to fdt_get_property_namelen(), but only examine the first
+ * namelen characters of name for matching the property name.
+ */
+const struct fdt_property *fdt_get_property_namelen(const void *fdt,
+ int nodeoffset,
+ const char *name,
+ int namelen, int *lenp);
+
+/**
+ * fdt_get_property - find a given property in a given node
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose property to find
+ * @name: name of the property to find
+ * @lenp: pointer to an integer variable (will be overwritten) or NULL
+ *
+ * fdt_get_property() retrieves a pointer to the fdt_property
+ * structure within the device tree blob corresponding to the property
+ * named 'name' of the node at offset nodeoffset. If lenp is
+ * non-NULL, the length of the property value is also returned, in the
+ * integer pointed to by lenp.
+ *
+ * returns:
+ * pointer to the structure representing the property
+ * if lenp is non-NULL, *lenp contains the length of the property
+ * value (>=0)
+ * NULL, on error
+ * if lenp is non-NULL, *lenp contains an error code (<0):
+ * -FDT_ERR_NOTFOUND, node does not have named property
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
+ const char *name, int *lenp);
+static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
+ const char *name,
+ int *lenp)
+{
+ return (struct fdt_property *)(uintptr_t)
+ fdt_get_property(fdt, nodeoffset, name, lenp);
+}
+
+/**
+ * fdt_getprop_by_offset - retrieve the value of a property at a given offset
+ * @fdt: pointer to the device tree blob
+ * @ffset: offset of the property to read
+ * @namep: pointer to a string variable (will be overwritten) or NULL
+ * @lenp: pointer to an integer variable (will be overwritten) or NULL
+ *
+ * fdt_getprop_by_offset() retrieves a pointer to the value of the
+ * property at structure block offset 'offset' (this will be a pointer
+ * to within the device blob itself, not a copy of the value). If
+ * lenp is non-NULL, the length of the property value is also
+ * returned, in the integer pointed to by lenp. If namep is non-NULL,
+ * the property's namne will also be returned in the char * pointed to
+ * by namep (this will be a pointer to within the device tree's string
+ * block, not a new copy of the name).
+ *
+ * returns:
+ * pointer to the property's value
+ * if lenp is non-NULL, *lenp contains the length of the property
+ * value (>=0)
+ * if namep is non-NULL *namep contiains a pointer to the property
+ * name.
+ * NULL, on error
+ * if lenp is non-NULL, *lenp contains an error code (<0):
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+const void *fdt_getprop_by_offset(const void *fdt, int offset,
+ const char **namep, int *lenp);
+
+/**
+ * fdt_getprop_namelen - get property value based on substring
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose property to find
+ * @name: name of the property to find
+ * @namelen: number of characters of name to consider
+ * @lenp: pointer to an integer variable (will be overwritten) or NULL
+ *
+ * Identical to fdt_getprop(), but only examine the first namelen
+ * characters of name for matching the property name.
+ */
+const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
+ const char *name, int namelen, int *lenp);
+
+/**
+ * fdt_getprop - retrieve the value of a given property
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose property to find
+ * @name: name of the property to find
+ * @lenp: pointer to an integer variable (will be overwritten) or NULL
+ *
+ * fdt_getprop() retrieves a pointer to the value of the property
+ * named 'name' of the node at offset nodeoffset (this will be a
+ * pointer to within the device blob itself, not a copy of the value).
+ * If lenp is non-NULL, the length of the property value is also
+ * returned, in the integer pointed to by lenp.
+ *
+ * returns:
+ * pointer to the property's value
+ * if lenp is non-NULL, *lenp contains the length of the property
+ * value (>=0)
+ * NULL, on error
+ * if lenp is non-NULL, *lenp contains an error code (<0):
+ * -FDT_ERR_NOTFOUND, node does not have named property
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+const void *fdt_getprop(const void *fdt, int nodeoffset,
+ const char *name, int *lenp);
+static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
+ const char *name, int *lenp)
+{
+ return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
+}
+
+/**
+ * fdt_get_phandle - retrieve the phandle of a given node
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: structure block offset of the node
+ *
+ * fdt_get_phandle() retrieves the phandle of the device tree node at
+ * structure block offset nodeoffset.
+ *
+ * returns:
+ * the phandle of the node at nodeoffset, on success (!= 0, != -1)
+ * 0, if the node has no phandle, or another error occurs
+ */
+uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
+
+/**
+ * fdt_get_alias_namelen - get alias based on substring
+ * @fdt: pointer to the device tree blob
+ * @name: name of the alias th look up
+ * @namelen: number of characters of name to consider
+ *
+ * Identical to fdt_get_alias(), but only examine the first namelen
+ * characters of name for matching the alias name.
+ */
+const char *fdt_get_alias_namelen(const void *fdt,
+ const char *name, int namelen);
+
+/**
+ * fdt_get_alias - retreive the path referenced by a given alias
+ * @fdt: pointer to the device tree blob
+ * @name: name of the alias th look up
+ *
+ * fdt_get_alias() retrieves the value of a given alias. That is, the
+ * value of the property named 'name' in the node /aliases.
+ *
+ * returns:
+ * a pointer to the expansion of the alias named 'name', of it exists
+ * NULL, if the given alias or the /aliases node does not exist
+ */
+const char *fdt_get_alias(const void *fdt, const char *name);
+
+/**
+ * fdt_get_path - determine the full path of a node
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose path to find
+ * @buf: character buffer to contain the returned path (will be overwritten)
+ * @buflen: size of the character buffer at buf
+ *
+ * fdt_get_path() computes the full path of the node at offset
+ * nodeoffset, and records that path in the buffer at buf.
+ *
+ * NOTE: This function is expensive, as it must scan the device tree
+ * structure from the start to nodeoffset.
+ *
+ * returns:
+ * 0, on success
+ * buf contains the absolute path of the node at
+ * nodeoffset, as a NUL-terminated string.
+ * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
+ * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
+ * characters and will not fit in the given buffer.
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE, standard meanings
+ */
+int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
+
+/**
+ * fdt_supernode_atdepth_offset - find a specific ancestor of a node
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose parent to find
+ * @supernodedepth: depth of the ancestor to find
+ * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
+ *
+ * fdt_supernode_atdepth_offset() finds an ancestor of the given node
+ * at a specific depth from the root (where the root itself has depth
+ * 0, its immediate subnodes depth 1 and so forth). So
+ * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
+ * will always return 0, the offset of the root node. If the node at
+ * nodeoffset has depth D, then:
+ * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
+ * will return nodeoffset itself.
+ *
+ * NOTE: This function is expensive, as it must scan the device tree
+ * structure from the start to nodeoffset.
+ *
+ * returns:
+
+ * structure block offset of the node at node offset's ancestor
+ * of depth supernodedepth (>=0), on success
+ * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
+* -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of nodeoffset
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE, standard meanings
+ */
+int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
+ int supernodedepth, int *nodedepth);
+
+/**
+ * fdt_node_depth - find the depth of a given node
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose parent to find
+ *
+ * fdt_node_depth() finds the depth of a given node. The root node
+ * has depth 0, its immediate subnodes depth 1 and so forth.
+ *
+ * NOTE: This function is expensive, as it must scan the device tree
+ * structure from the start to nodeoffset.
+ *
+ * returns:
+ * depth of the node at nodeoffset (>=0), on success
+ * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE, standard meanings
+ */
+int fdt_node_depth(const void *fdt, int nodeoffset);
+
+/**
+ * fdt_parent_offset - find the parent of a given node
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose parent to find
+ *
+ * fdt_parent_offset() locates the parent node of a given node (that
+ * is, it finds the offset of the node which contains the node at
+ * nodeoffset as a subnode).
+ *
+ * NOTE: This function is expensive, as it must scan the device tree
+ * structure from the start to nodeoffset, *twice*.
+ *
+ * returns:
+ * structure block offset of the parent of the node at nodeoffset
+ * (>=0), on success
+ * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE, standard meanings
+ */
+int fdt_parent_offset(const void *fdt, int nodeoffset);
+
+/**
+ * fdt_node_offset_by_prop_value - find nodes with a given property value
+ * @fdt: pointer to the device tree blob
+ * @startoffset: only find nodes after this offset
+ * @propname: property name to check
+ * @propval: property value to search for
+ * @proplen: length of the value in propval
+ *
+ * fdt_node_offset_by_prop_value() returns the offset of the first
+ * node after startoffset, which has a property named propname whose
+ * value is of length proplen and has value equal to propval; or if
+ * startoffset is -1, the very first such node in the tree.
+ *
+ * To iterate through all nodes matching the criterion, the following
+ * idiom can be used:
+ * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
+ * propval, proplen);
+ * while (offset != -FDT_ERR_NOTFOUND) {
+ * // other code here
+ * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
+ * propval, proplen);
+ * }
+ *
+ * Note the -1 in the first call to the function, if 0 is used here
+ * instead, the function will never locate the root node, even if it
+ * matches the criterion.
+ *
+ * returns:
+ * structure block offset of the located node (>= 0, >startoffset),
+ * on success
+ * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
+ * tree after startoffset
+ * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE, standard meanings
+ */
+int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
+ const char *propname,
+ const void *propval, int proplen);
+
+/**
+ * fdt_node_offset_by_phandle - find the node with a given phandle
+ * @fdt: pointer to the device tree blob
+ * @phandle: phandle value
+ *
+ * fdt_node_offset_by_phandle() returns the offset of the node
+ * which has the given phandle value. If there is more than one node
+ * in the tree with the given phandle (an invalid tree), results are
+ * undefined.
+ *
+ * returns:
+ * structure block offset of the located node (>= 0), on success
+ * -FDT_ERR_NOTFOUND, no node with that phandle exists
+ * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE, standard meanings
+ */
+int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
+
+/**
+ * fdt_node_check_compatible: check a node's compatible property
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of a tree node
+ * @compatible: string to match against
+ *
+ *
+ * fdt_node_check_compatible() returns 0 if the given node contains a
+ * 'compatible' property with the given string as one of its elements,
+ * it returns non-zero otherwise, or on error.
+ *
+ * returns:
+ * 0, if the node has a 'compatible' property listing the given string
+ * 1, if the node has a 'compatible' property, but it does not list
+ * the given string
+ * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
+ * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE, standard meanings
+ */
+int fdt_node_check_compatible(const void *fdt, int nodeoffset,
+ const char *compatible);
+
+/**
+ * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
+ * @fdt: pointer to the device tree blob
+ * @startoffset: only find nodes after this offset
+ * @compatible: 'compatible' string to match against
+ *
+ * fdt_node_offset_by_compatible() returns the offset of the first
+ * node after startoffset, which has a 'compatible' property which
+ * lists the given compatible string; or if startoffset is -1, the
+ * very first such node in the tree.
+ *
+ * To iterate through all nodes matching the criterion, the following
+ * idiom can be used:
+ * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
+ * while (offset != -FDT_ERR_NOTFOUND) {
+ * // other code here
+ * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
+ * }
+ *
+ * Note the -1 in the first call to the function, if 0 is used here
+ * instead, the function will never locate the root node, even if it
+ * matches the criterion.
+ *
+ * returns:
+ * structure block offset of the located node (>= 0, >startoffset),
+ * on success
+ * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
+ * tree after startoffset
+ * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE, standard meanings
+ */
+int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
+ const char *compatible);
+
+/**********************************************************************/
+/* Write-in-place functions */
+/**********************************************************************/
+
+/**
+ * fdt_setprop_inplace - change a property's value, but not its size
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose property to change
+ * @name: name of the property to change
+ * @val: pointer to data to replace the property value with
+ * @len: length of the property value
+ *
+ * fdt_setprop_inplace() replaces the value of a given property with
+ * the data in val, of length len. This function cannot change the
+ * size of a property, and so will only work if len is equal to the
+ * current length of the property.
+ *
+ * This function will alter only the bytes in the blob which contain
+ * the given property value, and will not alter or move any other part
+ * of the tree.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
+ * -FDT_ERR_NOTFOUND, node does not have the named property
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
+ const void *val, int len);
+
+/**
+ * fdt_setprop_inplace_cell - change the value of a single-cell property
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose property to change
+ * @name: name of the property to change
+ * @val: cell (32-bit integer) value to replace the property with
+ *
+ * fdt_setprop_inplace_cell() replaces the value of a given property
+ * with the 32-bit integer cell value in val, converting val to
+ * big-endian if necessary. This function cannot change the size of a
+ * property, and so will only work if the property already exists and
+ * has length 4.
+ *
+ * This function will alter only the bytes in the blob which contain
+ * the given property value, and will not alter or move any other part
+ * of the tree.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
+ * -FDT_ERR_NOTFOUND, node does not have the named property
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
+ const char *name, uint32_t val)
+{
+ val = cpu_to_fdt32(val);
+ return fdt_setprop_inplace(fdt, nodeoffset, name, &val, sizeof(val));
+}
+
+/**
+ * fdt_nop_property - replace a property with nop tags
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose property to nop
+ * @name: name of the property to nop
+ *
+ * fdt_nop_property() will replace a given property's representation
+ * in the blob with FDT_NOP tags, effectively removing it from the
+ * tree.
+ *
+ * This function will alter only the bytes in the blob which contain
+ * the property, and will not alter or move any other part of the
+ * tree.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_NOTFOUND, node does not have the named property
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
+
+/**
+ * fdt_nop_node - replace a node (subtree) with nop tags
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node to nop
+ *
+ * fdt_nop_node() will replace a given node's representation in the
+ * blob, including all its subnodes, if any, with FDT_NOP tags,
+ * effectively removing it from the tree.
+ *
+ * This function will alter only the bytes in the blob which contain
+ * the node and its properties and subnodes, and will not alter or
+ * move any other part of the tree.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+int fdt_nop_node(void *fdt, int nodeoffset);
+
+/**********************************************************************/
+/* Sequential write functions */
+/**********************************************************************/
+
+int fdt_create(void *buf, int bufsize);
+int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
+int fdt_finish_reservemap(void *fdt);
+int fdt_begin_node(void *fdt, const char *name);
+int fdt_property(void *fdt, const char *name, const void *val, int len);
+static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
+{
+ val = cpu_to_fdt32(val);
+ return fdt_property(fdt, name, &val, sizeof(val));
+}
+#define fdt_property_string(fdt, name, str) \
+ fdt_property(fdt, name, str, strlen(str)+1)
+int fdt_end_node(void *fdt);
+int fdt_finish(void *fdt);
+
+/**********************************************************************/
+/* Read-write functions */
+/**********************************************************************/
+
+int fdt_open_into(const void *fdt, void *buf, int bufsize);
+int fdt_pack(void *fdt);
+
+/**
+ * fdt_add_mem_rsv - add one memory reserve map entry
+ * @fdt: pointer to the device tree blob
+ * @address, @size: 64-bit values (native endian)
+ *
+ * Adds a reserve map entry to the given blob reserving a region at
+ * address address of length size.
+ *
+ * This function will insert data into the reserve map and will
+ * therefore change the indexes of some entries in the table.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
+ * contain the new reservation entry
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_BADLAYOUT,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
+
+/**
+ * fdt_del_mem_rsv - remove a memory reserve map entry
+ * @fdt: pointer to the device tree blob
+ * @n: entry to remove
+ *
+ * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
+ * the blob.
+ *
+ * This function will delete data from the reservation table and will
+ * therefore change the indexes of some entries in the table.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
+ * are less than n+1 reserve map entries)
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_BADLAYOUT,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+int fdt_del_mem_rsv(void *fdt, int n);
+
+/**
+ * fdt_set_name - change the name of a given node
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: structure block offset of a node
+ * @name: name to give the node
+ *
+ * fdt_set_name() replaces the name (including unit address, if any)
+ * of the given node with the given string. NOTE: this function can't
+ * efficiently check if the new name is unique amongst the given
+ * node's siblings; results are undefined if this function is invoked
+ * with a name equal to one of the given node's siblings.
+ *
+ * This function may insert or delete data from the blob, and will
+ * therefore change the offsets of some existing nodes.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
+ * to contain the new name
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE, standard meanings
+ */
+int fdt_set_name(void *fdt, int nodeoffset, const char *name);
+
+/**
+ * fdt_setprop - create or change a property
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose property to change
+ * @name: name of the property to change
+ * @val: pointer to data to set the property value to
+ * @len: length of the property value
+ *
+ * fdt_setprop() sets the value of the named property in the given
+ * node to the given value and length, creating the property if it
+ * does not already exist.
+ *
+ * This function may insert or delete data from the blob, and will
+ * therefore change the offsets of some existing nodes.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
+ * contain the new property value
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADLAYOUT,
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_BADLAYOUT,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+int fdt_setprop(void *fdt, int nodeoffset, const char *name,
+ const void *val, int len);
+
+/**
+ * fdt_setprop_cell - set a property to a single cell value
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose property to change
+ * @name: name of the property to change
+ * @val: 32-bit integer value for the property (native endian)
+ *
+ * fdt_setprop_cell() sets the value of the named property in the
+ * given node to the given cell value (converting to big-endian if
+ * necessary), or creates a new property with that value if it does
+ * not already exist.
+ *
+ * This function may insert or delete data from the blob, and will
+ * therefore change the offsets of some existing nodes.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
+ * contain the new property value
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADLAYOUT,
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_BADLAYOUT,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
+ uint32_t val)
+{
+ val = cpu_to_fdt32(val);
+ return fdt_setprop(fdt, nodeoffset, name, &val, sizeof(val));
+}
+
+/**
+ * fdt_setprop_string - set a property to a string value
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose property to change
+ * @name: name of the property to change
+ * @str: string value for the property
+ *
+ * fdt_setprop_string() sets the value of the named property in the
+ * given node to the given string value (using the length of the
+ * string to determine the new length of the property), or creates a
+ * new property with that value if it does not already exist.
+ *
+ * This function may insert or delete data from the blob, and will
+ * therefore change the offsets of some existing nodes.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
+ * contain the new property value
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADLAYOUT,
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_BADLAYOUT,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+#define fdt_setprop_string(fdt, nodeoffset, name, str) \
+ fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
+
+/**
+ * fdt_delprop - delete a property
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node whose property to nop
+ * @name: name of the property to nop
+ *
+ * fdt_del_property() will delete the given property.
+ *
+ * This function will delete data from the blob, and will therefore
+ * change the offsets of some existing nodes.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_NOTFOUND, node does not have the named property
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADLAYOUT,
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+int fdt_delprop(void *fdt, int nodeoffset, const char *name);
+
+/**
+ * fdt_add_subnode_namelen - creates a new node based on substring
+ * @fdt: pointer to the device tree blob
+ * @parentoffset: structure block offset of a node
+ * @name: name of the subnode to locate
+ * @namelen: number of characters of name to consider
+ *
+ * Identical to fdt_add_subnode(), but use only the first namelen
+ * characters of name as the name of the new node. This is useful for
+ * creating subnodes based on a portion of a larger string, such as a
+ * full path.
+ */
+int fdt_add_subnode_namelen(void *fdt, int parentoffset,
+ const char *name, int namelen);
+
+/**
+ * fdt_add_subnode - creates a new node
+ * @fdt: pointer to the device tree blob
+ * @parentoffset: structure block offset of a node
+ * @name: name of the subnode to locate
+ *
+ * fdt_add_subnode() creates a new node as a subnode of the node at
+ * structure block offset parentoffset, with the given name (which
+ * should include the unit address, if any).
+ *
+ * This function will insert data into the blob, and will therefore
+ * change the offsets of some existing nodes.
+
+ * returns:
+ * structure block offset of the created nodeequested subnode (>=0), on success
+ * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
+ * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
+ * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
+ * the given name
+ * -FDT_ERR_NOSPACE, if there is insufficient free space in the
+ * blob to contain the new node
+ * -FDT_ERR_NOSPACE
+ * -FDT_ERR_BADLAYOUT
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings.
+ */
+int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
+
+/**
+ * fdt_del_node - delete a node (subtree)
+ * @fdt: pointer to the device tree blob
+ * @nodeoffset: offset of the node to nop
+ *
+ * fdt_del_node() will remove the given node, including all its
+ * subnodes if any, from the blob.
+ *
+ * This function will delete data from the blob, and will therefore
+ * change the offsets of some existing nodes.
+ *
+ * returns:
+ * 0, on success
+ * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
+ * -FDT_ERR_BADLAYOUT,
+ * -FDT_ERR_BADMAGIC,
+ * -FDT_ERR_BADVERSION,
+ * -FDT_ERR_BADSTATE,
+ * -FDT_ERR_BADSTRUCTURE,
+ * -FDT_ERR_TRUNCATED, standard meanings
+ */
+int fdt_del_node(void *fdt, int nodeoffset);
+
+/**********************************************************************/
+/* Debugging / informational functions */
+/**********************************************************************/
+
+const char *fdt_strerror(int errval);
+
+#endif /* _LIBFDT_H */
diff -purN trunk/grub-core/lib/dtc/libfdt/libfdt_internal.h arm-uboot/grub-core/lib/dtc/libfdt/libfdt_internal.h
--- trunk/grub-core/lib/dtc/libfdt/libfdt_internal.h 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/libfdt_internal.h 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,95 @@
+#ifndef _LIBFDT_INTERNAL_H
+#define _LIBFDT_INTERNAL_H
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Copyright (C) 2006 David Gibson, IBM Corporation.
+ *
+ * libfdt is dual licensed: you can use it either under the terms of
+ * the GPL, or the BSD license, at your option.
+ *
+ * a) This library 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.
+ *
+ * This library 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 library; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ * Alternatively,
+ *
+ * b) Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <fdt.h>
+
+#define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
+#define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE))
+
+#define FDT_CHECK_HEADER(fdt) \
+ { \
+ int err; \
+ if ((err = fdt_check_header(fdt)) != 0) \
+ return err; \
+ }
+
+int _fdt_check_node_offset(const void *fdt, int offset);
+int _fdt_check_prop_offset(const void *fdt, int offset);
+const char *_fdt_find_string(const char *strtab, int tabsize, const char *s);
+int _fdt_node_end_offset(void *fdt, int nodeoffset);
+
+static inline const void *_fdt_offset_ptr(const void *fdt, int offset)
+{
+ return (const char *)fdt + fdt_off_dt_struct(fdt) + offset;
+}
+
+static inline void *_fdt_offset_ptr_w(void *fdt, int offset)
+{
+ return (void *)(uintptr_t)_fdt_offset_ptr(fdt, offset);
+}
+
+static inline const struct fdt_reserve_entry *_fdt_mem_rsv(const void *fdt, int n)
+{
+ const struct fdt_reserve_entry *rsv_table =
+ (const struct fdt_reserve_entry *)
+ ((const char *)fdt + fdt_off_mem_rsvmap(fdt));
+
+ return rsv_table + n;
+}
+static inline struct fdt_reserve_entry *_fdt_mem_rsv_w(void *fdt, int n)
+{
+ return (void *)(uintptr_t)_fdt_mem_rsv(fdt, n);
+}
+
+#define FDT_SW_MAGIC (~FDT_MAGIC)
+
+#endif /* _LIBFDT_INTERNAL_H */
diff -purN trunk/grub-core/lib/dtc/libfdt/Makefile.libfdt arm-uboot/grub-core/lib/dtc/libfdt/Makefile.libfdt
--- trunk/grub-core/lib/dtc/libfdt/Makefile.libfdt 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/Makefile.libfdt 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,10 @@
+# Makefile.libfdt
+#
+# This is not a complete Makefile of itself. Instead, it is designed to
+# be easily embeddable into other systems of Makefiles.
+#
+LIBFDT_soname = libfdt.$(SHAREDLIB_EXT).1
+LIBFDT_INCLUDES = fdt.h libfdt.h
+LIBFDT_VERSION = version.lds
+LIBFDT_SRCS = fdt.c fdt_ro.c fdt_wip.c fdt_sw.c fdt_rw.c fdt_strerror.c
+LIBFDT_OBJS = $(LIBFDT_SRCS:%.c=%.o)
diff -purN trunk/grub-core/lib/dtc/libfdt/TODO arm-uboot/grub-core/lib/dtc/libfdt/TODO
--- trunk/grub-core/lib/dtc/libfdt/TODO 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/TODO 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,3 @@
+- Tree traversal functions
+- Graft function
+- Complete libfdt.h documenting comments
diff -purN trunk/grub-core/lib/dtc/libfdt/version.lds arm-uboot/grub-core/lib/dtc/libfdt/version.lds
--- trunk/grub-core/lib/dtc/libfdt/version.lds 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt/version.lds 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,54 @@
+LIBFDT_1.2 {
+ global:
+ fdt_next_node;
+ fdt_check_header;
+ fdt_move;
+ fdt_string;
+ fdt_num_mem_rsv;
+ fdt_get_mem_rsv;
+ fdt_subnode_offset_namelen;
+ fdt_subnode_offset;
+ fdt_path_offset;
+ fdt_get_name;
+ fdt_get_property_namelen;
+ fdt_get_property;
+ fdt_getprop_namelen;
+ fdt_getprop;
+ fdt_get_phandle;
+ fdt_get_alias_namelen;
+ fdt_get_alias;
+ fdt_get_path;
+ fdt_supernode_atdepth_offset;
+ fdt_node_depth;
+ fdt_parent_offset;
+ fdt_node_offset_by_prop_value;
+ fdt_node_offset_by_phandle;
+ fdt_node_check_compatible;
+ fdt_node_offset_by_compatible;
+ fdt_setprop_inplace;
+ fdt_nop_property;
+ fdt_nop_node;
+ fdt_create;
+ fdt_add_reservemap_entry;
+ fdt_finish_reservemap;
+ fdt_begin_node;
+ fdt_property;
+ fdt_end_node;
+ fdt_finish;
+ fdt_open_into;
+ fdt_pack;
+ fdt_add_mem_rsv;
+ fdt_del_mem_rsv;
+ fdt_set_name;
+ fdt_setprop;
+ fdt_delprop;
+ fdt_add_subnode_namelen;
+ fdt_add_subnode;
+ fdt_del_node;
+ fdt_strerror;
+ fdt_offset_ptr;
+ fdt_next_tag;
+
+ local:
+ *;
+};
diff -purN trunk/grub-core/lib/dtc/libfdt-grub.diff arm-uboot/grub-core/lib/dtc/libfdt-grub.diff
--- trunk/grub-core/lib/dtc/libfdt-grub.diff 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/dtc/libfdt-grub.diff 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,63 @@
+diff -purN libfdt.orig/fdt_rw.c libfdt/fdt_rw.c
+--- libfdt.orig/fdt_rw.c 2011-05-08 20:45:39.000000000 +0100
++++ libfdt/fdt_rw.c 2012-10-19 15:33:11.085523185 +0100
+@@ -88,9 +88,9 @@ static int _fdt_rw_check_header(void *fd
+
+ #define FDT_RW_CHECK_HEADER(fdt) \
+ { \
+- int err; \
+- if ((err = _fdt_rw_check_header(fdt)) != 0) \
+- return err; \
++ int macro_err; \
++ if ((macro_err = _fdt_rw_check_header(fdt)) != 0) \
++ return macro_err; \
+ }
+
+ static inline int _fdt_data_size(void *fdt)
+diff -purN libfdt.orig/libfdt_env.h libfdt/libfdt_env.h
+--- libfdt.orig/libfdt_env.h 2011-05-08 20:45:39.000000000 +0100
++++ libfdt/libfdt_env.h 2012-10-19 16:13:19.051344173 +0100
+@@ -4,9 +4,27 @@
+ #ifndef _LIBFDT_ENV_H
+ #define _LIBFDT_ENV_H
+
++#ifndef GRUB_MACHINE
++/* Used when building for an ANSI C platform */
+ #include <stddef.h>
+ #include <stdint.h>
+ #include <string.h>
++#else
++/* Achieving the came for GRUB */
++#include <grub/misc.h>
++#include <grub/mm.h>
++#include <grub/types.h>
++typedef grub_size_t size_t;
++typedef grub_uint8_t uint8_t;
++typedef grub_uint32_t uint32_t;
++typedef grub_uint64_t uint64_t;
++typedef grub_addr_t uintptr_t;
++#define memchr grub_memchr
++#define strlen grub_strlen
++#define strchr grub_strchr
++#pragma GCC diagnostic ignored "-Wcast-align"
++#pragma GCC diagnostic ignored "-Wsign-compare"
++#endif
+
+ #define _B(n) ((unsigned long long)((uint8_t *)&x)[n])
+ static inline uint32_t fdt32_to_cpu(uint32_t x)
+diff -purN libfdt.orig/libfdt_internal.h libfdt/libfdt_internal.h
+--- libfdt.orig/libfdt_internal.h 2011-05-08 20:45:39.000000000 +0100
++++ libfdt/libfdt_internal.h 2012-10-19 15:33:11.105524731 +0100
+@@ -60,9 +60,9 @@
+
+ #define FDT_CHECK_HEADER(fdt) \
+ { \
+- int err; \
+- if ((err = fdt_check_header(fdt)) != 0) \
+- return err; \
++ int macro_err; \
++ if ((macro_err = fdt_check_header(fdt)) != 0) \
++ return macro_err; \
+ }
+
+ int _fdt_check_node_offset(const void *fdt, int offset);
diff -purN trunk/grub-core/lib/setjmp.S arm-uboot/grub-core/lib/setjmp.S
--- trunk/grub-core/lib/setjmp.S 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/grub-core/lib/setjmp.S 2012-11-20 16:26:54.950920000 +0000
@@ -11,6 +11,8 @@
#elif defined(__ia64__)
#include "./ia64/setjmp.S"
#include "./ia64/longjmp.S"
+#elif defined(__arm__)
+#include "./arm/setjmp.S"
#else
#error "Unknown target cpu type"
#endif
diff -purN trunk/grub-core/lib/uboot/datetime.c arm-uboot/grub-core/lib/uboot/datetime.c
--- trunk/grub-core/lib/uboot/datetime.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/uboot/datetime.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,41 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/types.h>
+#include <grub/symbol.h>
+#include <grub/uboot/uboot.h>
+#include <grub/datetime.h>
+#include <grub/dl.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* No simple platform-independent RTC access exists in U-Boot. */
+
+grub_err_t
+grub_get_datetime (struct grub_datetime *datetime __attribute__ ((unused)))
+{
+ return grub_error (GRUB_ERR_INVALID_COMMAND,
+ "can\'t get datetime using U-Boot");
+}
+
+grub_err_t
+grub_set_datetime (struct grub_datetime * datetime __attribute__ ((unused)))
+{
+ return grub_error (GRUB_ERR_INVALID_COMMAND,
+ "can\'t set datetime using U-Boot");
+}
diff -purN trunk/grub-core/lib/uboot/halt.c arm-uboot/grub-core/lib/uboot/halt.c
--- trunk/grub-core/lib/uboot/halt.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/uboot/halt.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,31 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/kernel.h>
+
+void
+grub_halt (void)
+{
+ grub_machine_fini ();
+
+ /* Just stop here */
+
+ while (1);
+}
diff -purN trunk/grub-core/lib/uboot/reboot.c arm-uboot/grub-core/lib/uboot/reboot.c
--- trunk/grub-core/lib/uboot/reboot.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/lib/uboot/reboot.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,30 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/misc.h>
+#include <grub/uboot/uboot.h>
+
+void
+grub_reboot (void)
+{
+ grub_machine_fini ();
+
+ uboot_reset ();
+ while (1);
+}
diff -purN trunk/grub-core/loader/arm/uboot/linux.c arm-uboot/grub-core/loader/arm/uboot/linux.c
--- trunk/grub-core/loader/arm/uboot/linux.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/loader/arm/uboot/linux.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,379 @@
+/* linux.c - boot Linux */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/dl.h>
+#include <grub/file.h>
+#include <grub/loader.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/command.h>
+#include <grub/cache.h>
+#include <grub/cpu/linux.h>
+#include <grub/lib/cmdline.h>
+#include <grub/uboot/uboot.h>
+
+#include <libfdt.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_dl_t my_mod;
+
+static grub_addr_t initrd_start;
+static grub_size_t initrd_end;
+
+static grub_addr_t linux_addr;
+static grub_size_t linux_size;
+
+static char *linux_args;
+
+static grub_addr_t firmware_boot_data;
+static grub_addr_t boot_data;
+static grub_uint32_t machine_type;
+
+/*
+ * linux_prepare_fdt():
+ * Prepares a loaded FDT for being passed to Linux.
+ * Merges in command line parameters and sets up initrd addresses.
+ */
+static grub_err_t
+linux_prepare_fdt (void)
+{
+ int node;
+ int retval;
+ int tmp_size;
+ void *tmp_fdt;
+
+ tmp_size = fdt_totalsize ((void *) boot_data) + FDT_ADDITIONAL_ENTRIES_SIZE;
+ tmp_fdt = grub_malloc (tmp_size);
+ if (!tmp_fdt)
+ return GRUB_ERR_OUT_OF_MEMORY;
+
+ fdt_open_into ((void *) boot_data, tmp_fdt, tmp_size);
+
+ /* Find or create '/chosen' node */
+ node = fdt_subnode_offset (tmp_fdt, 0, "chosen");
+ if (node < 0)
+ {
+ grub_printf ("No 'chosen' node in FDT - creating.\n");
+ node = fdt_add_subnode (tmp_fdt, 0, "chosen");
+ if (node < 0)
+ goto failure;
+ }
+
+ grub_printf ("linux_args: '%s'\n", linux_args);
+
+ /* Generate and set command line */
+ retval = fdt_setprop (tmp_fdt, node, "bootargs", linux_args,
+ grub_strlen (linux_args) + 1);
+ if (retval)
+ goto failure;
+
+ if (initrd_start && initrd_end)
+ {
+ /*
+ * We're using physical addresses, so even if we have LPAE, we're
+ * restricted to a 32-bit address space.
+ */
+ grub_uint32_t fdt_initrd_start = cpu_to_fdt32 (initrd_start);
+ grub_uint32_t fdt_initrd_end = cpu_to_fdt32 (initrd_end);
+
+ grub_dprintf ("loader", "Initrd @ 0x%08x-0x%08x\n",
+ initrd_start, initrd_end);
+
+ retval = fdt_setprop (tmp_fdt, node, "linux,initrd-start",
+ &fdt_initrd_start, sizeof (fdt_initrd_start));
+ if (retval)
+ goto failure;
+ retval = fdt_setprop (tmp_fdt, node, "linux,initrd-end",
+ &fdt_initrd_end, sizeof (fdt_initrd_end));
+ if (retval)
+ goto failure;
+ }
+
+ /* Copy updated FDT to its launch location */
+ fdt_move (tmp_fdt, (void *) boot_data, fdt_totalsize (tmp_fdt));
+ grub_free (tmp_fdt);
+ fdt_pack ((void *) boot_data);
+
+ grub_dprintf ("loader", "FDT updated for Linux boot\n");
+
+ return GRUB_ERR_NONE;
+
+failure:
+ grub_free (tmp_fdt);
+ return GRUB_ERR_BAD_ARGUMENT;
+}
+
+static grub_err_t
+linux_boot (void)
+{
+ kernel_entry_t linuxmain;
+
+ grub_arch_sync_caches ((void *) linux_addr, linux_size);
+
+ grub_dprintf ("loader", "Kernel at: 0x%x\n", linux_addr);
+
+ if (!boot_data)
+ {
+ if (firmware_boot_data)
+ {
+ grub_printf ("Using firmware-supplied boot data @ 0x%08x\n",
+ firmware_boot_data);
+ boot_data = firmware_boot_data;
+ }
+ else
+ {
+ return GRUB_ERR_FILE_NOT_FOUND;
+ }
+ }
+
+ grub_dprintf ("loader", "Boot data at: 0x%x\n", boot_data);
+
+ if (fdt32_to_cpu (*(grub_uint32_t *) (boot_data)) == FDT_MAGIC)
+ {
+ grub_dprintf ("loader", "FDT @ 0x%08x\n", (grub_addr_t) boot_data);
+ if (linux_prepare_fdt () != GRUB_ERR_NONE)
+ {
+ grub_dprintf ("loader", "linux_prepare_fdt() failed\n");
+ return GRUB_ERR_FILE_NOT_FOUND;
+ }
+ }
+
+ grub_dprintf ("loader", "Jumping to Linux...\n");
+
+ /* Boot the kernel.
+ * Arguments to kernel:
+ * r0 - 0
+ * r1 - machine type (passed from U-Boot)
+ * r2 - address of DTB or ATAG list
+ */
+ linuxmain = (kernel_entry_t) linux_addr;
+ linuxmain (0, machine_type, (void *) boot_data);
+
+ return GRUB_ERR_NONE;
+}
+
+/*
+ * Only support zImage, so no relocations necessary
+ */
+static grub_err_t
+linux_load (const char *filename)
+{
+ grub_file_t file;
+ int size;
+
+ file = grub_file_open (filename);
+ if (!file)
+ return GRUB_ERR_FILE_NOT_FOUND;
+
+ size = grub_file_size (file);
+ if (size == 0)
+ return GRUB_ERR_FILE_READ_ERROR;
+
+ linux_addr = LINUX_ADDRESS;
+ grub_dprintf ("loader", "Loading Linux to 0x%08x\n",
+ (grub_addr_t) linux_addr);
+
+ if (grub_file_read (file, (void *) linux_addr, size) != size)
+ {
+ grub_printf ("Kernel read failed!\n");
+ return GRUB_ERR_FILE_READ_ERROR;
+ }
+
+ if (*(grub_uint32_t *) (linux_addr + LINUX_ZIMAGE_OFFSET)
+ != LINUX_ZIMAGE_MAGIC)
+ {
+ return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("invalid zImage"));
+ }
+
+ linux_size = size;
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+linux_unload (void)
+{
+ grub_dl_unref (my_mod);
+
+ grub_free (linux_args);
+ linux_args = NULL;
+
+ initrd_start = initrd_end = 0;
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
+ int argc, char *argv[])
+{
+ int size, retval;
+ grub_file_t file;
+ grub_dl_ref (my_mod);
+
+ if (argc == 0)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
+
+ file = grub_file_open (argv[0]);
+ if (!file)
+ goto fail;
+
+ retval = linux_load (argv[0]);
+ grub_file_close (file);
+ if (retval != GRUB_ERR_NONE)
+ goto fail;
+
+ grub_loader_set (linux_boot, linux_unload, 1);
+
+ size = grub_loader_cmdline_size (argc, argv);
+ linux_args = grub_malloc (size + sizeof (LINUX_IMAGE));
+ if (!linux_args)
+ goto fail;
+
+ /* Create kernel command line. */
+ grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
+ grub_create_loader_cmdline (argc, argv,
+ linux_args + sizeof (LINUX_IMAGE) - 1, size);
+
+ return GRUB_ERR_NONE;
+
+fail:
+ grub_dl_unref (my_mod);
+ return grub_errno;
+}
+
+static grub_err_t
+grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
+ int argc, char *argv[])
+{
+ grub_file_t file;
+ int size;
+
+ if (argc == 0)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
+
+ file = grub_file_open (argv[0]);
+ if (!file)
+ return grub_errno;
+
+ size = grub_file_size (file);
+ if (size == 0)
+ goto fail;
+
+ initrd_start = LINUX_INITRD_ADDRESS;
+ grub_dprintf ("loader", "Loading initrd to 0x%08x\n",
+ (grub_addr_t) initrd_start);
+
+ if (grub_file_read (file, (void *) initrd_start, size) != size)
+ goto fail;
+
+ initrd_end = initrd_start + size;
+
+ return GRUB_ERR_NONE;
+
+fail:
+ grub_file_close (file);
+
+ return grub_errno;
+}
+
+static void *
+load_dtb (const char *path)
+{
+ grub_file_t dtb;
+ int size;
+ void *fdt = NULL;
+
+ dtb = grub_file_open (path);
+ if (!dtb)
+ return NULL;
+
+ size = grub_file_size (dtb);
+ if (size == 0)
+ goto out;
+
+ fdt = grub_malloc (size);
+ if (!fdt)
+ goto out;
+
+ if (grub_file_read (dtb, fdt, size) != size)
+ {
+ grub_free (fdt);
+ fdt = NULL;
+ goto out;
+ }
+
+ if (fdt_check_header (fdt) != 0)
+ {
+ grub_free (fdt);
+ fdt = NULL;
+ }
+
+out:
+ grub_file_close (dtb);
+
+ return fdt;
+}
+
+static grub_err_t
+grub_cmd_devicetree (grub_command_t cmd __attribute__ ((unused)),
+ int argc, char *argv[])
+{
+ void *blob;
+
+ if (argc != 1)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
+
+ blob = load_dtb (argv[0]);
+ if (!blob)
+ return GRUB_ERR_FILE_NOT_FOUND;
+
+ boot_data = LINUX_FDT_ADDRESS;
+ grub_dprintf ("loader", "Loading device tree to 0x%08x\n",
+ (grub_addr_t) boot_data);
+ fdt_move (blob, (void *) boot_data, fdt_totalsize (blob));
+ grub_free (blob);
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_command_t cmd_linux, cmd_initrd, cmd_devicetree;
+
+GRUB_MOD_INIT (linux)
+{
+ cmd_linux = grub_register_command ("linux", grub_cmd_linux,
+ 0, N_("Load Linux."));
+ cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
+ 0, N_("Load initrd."));
+ cmd_devicetree = grub_register_command ("devicetree", grub_cmd_devicetree,
+ 0, N_("Load DTB file."));
+ my_mod = mod;
+ firmware_boot_data = uboot_get_boot_data ();
+
+ boot_data = (grub_addr_t) NULL;
+ machine_type = uboot_get_machine_type ();
+}
+
+GRUB_MOD_FINI (linux)
+{
+ grub_unregister_command (cmd_linux);
+ grub_unregister_command (cmd_initrd);
+ grub_unregister_command (cmd_devicetree);
+}
diff -purN trunk/grub-core/Makefile.am arm-uboot/grub-core/Makefile.am
--- trunk/grub-core/Makefile.am 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/grub-core/Makefile.am 2012-11-20 16:26:54.950920000 +0000
@@ -143,6 +143,11 @@ KERNEL_HEADER_FILES += $(top_srcdir)/inc
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
endif
+if COND_arm_uboot
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/uboot/uboot.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/uboot/disk.h
+endif
+
if COND_mips
KERNEL_HEADER_FILES += $(top_builddir)/include/grub/cpu/kernel.h
endif
diff -purN trunk/grub-core/Makefile.core.def arm-uboot/grub-core/Makefile.core.def
--- trunk/grub-core/Makefile.core.def 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/grub-core/Makefile.core.def 2012-11-20 16:26:54.950920000 +0000
@@ -67,6 +67,9 @@ kernel = {
emu_cflags = '$(CFLAGS_GNULIB)';
emu_cppflags = '$(CPPFLAGS_GNULIB)';
+ arm_uboot_ldflags = '-Wl,-Ttext=0x08000000';
+ arm_uboot_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version';
+
i386_pc_startup = kern/i386/pc/startup.S;
i386_efi_startup = kern/i386/efi/startup.S;
x86_64_efi_startup = kern/x86_64/efi/startup.S;
@@ -77,6 +80,7 @@ kernel = {
mips_startup = kern/mips/startup.S;
sparc64_ieee1275_startup = kern/sparc64/ieee1275/crt0.S;
powerpc_ieee1275_startup = kern/powerpc/ieee1275/startup.S;
+ arm_uboot_startup = kern/arm/uboot/startup.S;
common = kern/command.c;
common = kern/corecmd.c;
@@ -199,6 +203,15 @@ kernel = {
sparc64_ieee1275 = kern/sparc64/dl.c;
sparc64_ieee1275 = kern/sparc64/ieee1275/ieee1275.c;
+ uboot = disk/uboot/ubootdisk.c;
+ uboot = kern/uboot/uboot.c;
+ uboot = kern/uboot/init.c;
+ uboot = kern/uboot/hw.c;
+ uboot = term/uboot/console.c;
+
+ arm = kern/arm/dl.c;
+ arm = lib/arm/libgcc.S;
+
emu = disk/host.c;
emu = gnulib/progname.c;
emu = gnulib/error.c;
@@ -595,6 +608,8 @@ module = {
module = {
name = cat;
common = commands/cat.c;
+ arm = lib/arg.c;
+ arm = commands/extcmd.c;
};
module = {
@@ -665,6 +680,7 @@ module = {
efi = lib/efi/halt.c;
ieee1275 = lib/ieee1275/halt.c;
emu = lib/emu/halt.c;
+ uboot = lib/uboot/halt.c;
};
module = {
@@ -678,6 +694,7 @@ module = {
mips_arc = lib/mips/arc/reboot.c;
mips_loongson = lib/mips/loongson/reboot.c;
mips_qemu_mips = lib/mips/qemu_mips/reboot.c;
+ uboot = lib/uboot/reboot.c;
common = commands/reboot.c;
};
@@ -1300,6 +1317,7 @@ module = {
name = datetime;
cmos = lib/cmos_datetime.c;
efi = lib/efi/datetime.c;
+ uboot = lib/uboot/datetime.c;
sparc64_ieee1275 = lib/ieee1275/datetime.c;
powerpc_ieee1275 = lib/ieee1275/datetime.c;
sparc64_ieee1275 = lib/ieee1275/cmos.c;
@@ -1319,6 +1337,7 @@ module = {
extra_dist = lib/powerpc/setjmp.S;
extra_dist = lib/ia64/setjmp.S;
extra_dist = lib/ia64/longjmp.S;
+ extra_dist = lib/arm/setjmp.S;
};
module = {
@@ -1397,8 +1416,12 @@ module = {
powerpc_ieee1275 = loader/powerpc/ieee1275/linux.c;
sparc64_ieee1275 = loader/sparc64/ieee1275/linux.c;
ia64_efi = loader/ia64/efi/linux.c;
+ arm_uboot = loader/arm/uboot/linux.c;
common = lib/cmdline.c;
enable = noemu;
+
+ fdt_cppflags = '$(CPPFLAGS_LIBFDT)';
+ fdt_ldadd = libfdtlib.a;
};
module = {
diff -purN trunk/grub-core/term/terminfo.c arm-uboot/grub-core/term/terminfo.c
--- trunk/grub-core/term/terminfo.c 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/grub-core/term/terminfo.c 2012-11-20 16:26:54.950920000 +0000
@@ -746,7 +746,9 @@ grub_cmd_terminfo (grub_extcmd_context_t
static grub_extcmd_t cmd;
-#if defined (GRUB_MACHINE_IEEE1275) || defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS) || defined (GRUB_MACHINE_ARC)
+#if defined (GRUB_MACHINE_IEEE1275) || defined (GRUB_MACHINE_MIPS_LOONGSON) || \
+ defined (GRUB_MACHINE_MIPS_QEMU_MIPS) || defined (GRUB_MACHINE_ARC) || \
+ defined (GRUB_MACHINE_UBOOT)
void grub_terminfo_init (void)
#else
GRUB_MOD_INIT(terminfo)
diff -purN trunk/grub-core/term/uboot/console.c arm-uboot/grub-core/term/uboot/console.c
--- trunk/grub-core/term/uboot/console.c 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/grub-core/term/uboot/console.c 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,143 @@
+/* console.c - console interface layer for U-Boot platforms */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/term.h>
+#include <grub/misc.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/terminfo.h>
+#include <grub/uboot/uboot.h>
+#include <grub/uboot/console.h>
+
+static void
+put (struct grub_term_output *term __attribute__ ((unused)), const int c)
+{
+ uboot_putc (c);
+}
+
+static int
+readkey (struct grub_term_input *term __attribute__ ((unused)))
+{
+ if (uboot_tstc () > 0)
+ return uboot_getc ();
+
+ return -1;
+}
+
+static void
+uboot_console_setcursor (struct grub_term_output *term
+ __attribute__ ((unused)), int on
+ __attribute__ ((unused)))
+{
+ grub_terminfo_setcursor (term, on);
+}
+
+static grub_err_t
+uboot_console_init_input (struct grub_term_input *term)
+{
+ return grub_terminfo_input_init (term);
+}
+
+extern struct grub_terminfo_output_state uboot_console_terminfo_output;
+
+static void
+uboot_console_dimensions (void)
+{
+ /* Use a small console by default. */
+ if (!uboot_console_terminfo_output.width)
+ uboot_console_terminfo_output.width = 80;
+ if (!uboot_console_terminfo_output.height)
+ uboot_console_terminfo_output.height = 24;
+}
+
+static grub_err_t
+uboot_console_init_output (struct grub_term_output *term)
+{
+ uboot_console_dimensions ();
+
+ grub_terminfo_output_init (term);
+
+ return 0;
+}
+
+struct grub_terminfo_input_state uboot_console_terminfo_input = {
+ .readkey = readkey
+};
+
+struct grub_terminfo_output_state uboot_console_terminfo_output = {
+ .put = put,
+ .width = 80,
+ .height = 24
+};
+
+static struct grub_term_input uboot_console_term_input = {
+ .name = "console",
+ .init = uboot_console_init_input,
+ .getkey = grub_terminfo_getkey,
+ .data = &uboot_console_terminfo_input
+};
+
+static struct grub_term_output uboot_console_term_output = {
+ .name = "console",
+ .init = uboot_console_init_output,
+ .putchar = grub_terminfo_putchar,
+ .getwh = grub_terminfo_getwh,
+ .getxy = grub_terminfo_getxy,
+ .gotoxy = grub_terminfo_gotoxy,
+ .cls = grub_terminfo_cls,
+ .setcolorstate = grub_terminfo_setcolorstate,
+ .setcursor = uboot_console_setcursor,
+ .flags = GRUB_TERM_CODE_TYPE_ASCII,
+ .data = &uboot_console_terminfo_output,
+ .normal_color = GRUB_TERM_DEFAULT_NORMAL_COLOR,
+ .highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR,
+};
+
+void
+grub_console_init_early (void)
+{
+ grub_term_register_input ("console", &uboot_console_term_input);
+ grub_term_register_output ("console", &uboot_console_term_output);
+}
+
+
+/*
+ * grub_console_init_lately():
+ * Initializes terminfo formatting by registering terminal type.
+ * Called after heap has been configured.
+ *
+ */
+void
+grub_console_init_lately (void)
+{
+ const char *type;
+
+ /* See if explicitly set by U-Boot environment */
+ type = uboot_env_get ("grub_term");
+ if (!type)
+ type = "vt100";
+
+ grub_terminfo_init ();
+ grub_terminfo_output_register (&uboot_console_term_output, type);
+}
+
+void
+grub_console_fini (void)
+{
+}
diff -purN trunk/include/grub/arm/linux.h arm-uboot/include/grub/arm/linux.h
--- trunk/include/grub/arm/linux.h 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/include/grub/arm/linux.h 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,34 @@
+/* linux.h - ARM linux specific definitions */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/>.
+ */
+
+#ifndef GRUB_LINUX_CPU_HEADER
+#define GRUB_LINUX_CPU_HEADER 1
+
+#define LINUX_ZIMAGE_OFFSET 0x24
+#define LINUX_ZIMAGE_MAGIC 0x016f2818
+
+#define LINUX_ADDRESS (start_of_ram + 0x8000)
+#define LINUX_INITRD_ADDRESS (start_of_ram + 0x02000000)
+#define LINUX_FDT_ADDRESS (LINUX_INITRD_ADDRESS - 0x10000)
+
+#define FDT_ADDITIONAL_ENTRIES_SIZE 0x300
+
+typedef void (*kernel_entry_t) (int, unsigned long, void *);
+
+#endif /* ! GRUB_LINUX_CPU_HEADER */
diff -purN trunk/include/grub/arm/time.h arm-uboot/include/grub/arm/time.h
--- trunk/include/grub/arm/time.h 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/include/grub/arm/time.h 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,29 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/>.
+ */
+
+#ifndef KERNEL_CPU_TIME_HEADER
+#define KERNEL_CPU_TIME_HEADER 1
+
+static __inline void
+grub_cpu_idle (void)
+{
+ /* FIXME: this can't work until we handle interrupts. */
+/* __asm__ __volatile__ ("wfi"); */
+}
+
+#endif /* ! KERNEL_CPU_TIME_HEADER */
diff -purN trunk/include/grub/arm/types.h arm-uboot/include/grub/arm/types.h
--- trunk/include/grub/arm/types.h 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/include/grub/arm/types.h 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,34 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/>.
+ */
+
+#ifndef GRUB_TYPES_CPU_HEADER
+#define GRUB_TYPES_CPU_HEADER 1
+
+/* The size of void *. */
+#define GRUB_TARGET_SIZEOF_VOID_P 4
+
+/* The size of long. */
+#define GRUB_TARGET_SIZEOF_LONG 4
+
+/* currently only support little-endian. */
+#undef GRUB_TARGET_WORDS_BIGENDIAN
+
+/* Unaligned accesses only supported if MMU enabled */
+//#define GRUB_HAVE_UNALIGNED_ACCESS 1
+
+#endif /* ! GRUB_TYPES_CPU_HEADER */
diff -purN trunk/include/grub/arm/uboot/kernel.h arm-uboot/include/grub/arm/uboot/kernel.h
--- trunk/include/grub/arm/uboot/kernel.h 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/include/grub/arm/uboot/kernel.h 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,32 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/>.
+ */
+
+#ifndef GRUB_KERNEL_MACHINE_HEADER
+#define GRUB_KERNEL_MACHINE_HEADER 1
+
+#ifndef ASM_FILE
+
+#include <grub/symbol.h>
+#include <grub/types.h>
+
+#endif /* ! ASM_FILE */
+
+#define GRUB_KERNEL_MACHINE_STACK_SIZE 0x40000
+#define GRUB_KERNEL_MACHINE_HEAP_SIZE (grub_size_t) (2 * 1024 * 1024)
+
+#endif /* ! GRUB_KERNEL_MACHINE_HEADER */
diff -purN trunk/include/grub/disk.h arm-uboot/include/grub/disk.h
--- trunk/include/grub/disk.h 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/include/grub/disk.h 2012-11-20 17:08:24.396460771 +0000
@@ -41,6 +41,7 @@ enum grub_disk_dev_id
GRUB_DISK_DEVICE_CRYPTODISK_ID,
GRUB_DISK_DEVICE_ARCDISK_ID,
GRUB_DISK_DEVICE_HOSTDISK_ID,
+ GRUB_DISK_DEVICE_UBOOTDISK_ID,
};
struct grub_disk;
diff -purN trunk/include/grub/elf.h arm-uboot/include/grub/elf.h
--- trunk/include/grub/elf.h 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/include/grub/elf.h 2012-11-20 16:26:54.950920000 +0000
@@ -145,6 +145,7 @@ typedef struct
#define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX. */
#define ELFOSABI_MODESTO 11 /* Novell Modesto. */
#define ELFOSABI_OPENBSD 12 /* OpenBSD. */
+#define ELFOSABI_ARM_AEABI 64 /* ARM EABI */
#define ELFOSABI_ARM 97 /* ARM */
#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
@@ -2005,15 +2006,18 @@ typedef Elf32_Addr Elf32_Conflict;
/* ARM specific declarations */
/* Processor specific flags for the ELF header e_flags field. */
-#define EF_ARM_RELEXEC 0x01
-#define EF_ARM_HASENTRY 0x02
-#define EF_ARM_INTERWORK 0x04
-#define EF_ARM_APCS_26 0x08
-#define EF_ARM_APCS_FLOAT 0x10
-#define EF_ARM_PIC 0x20
-#define EF_ARM_ALIGN8 0x40 /* 8-bit structure alignment is in use */
-#define EF_ARM_NEW_ABI 0x80
-#define EF_ARM_OLD_ABI 0x100
+#define EF_ARM_RELEXEC 0x01
+#define EF_ARM_HASENTRY 0x02
+#define EF_ARM_INTERWORK 0x04
+#define EF_ARM_APCS_26 0x08
+#define EF_ARM_APCS_FLOAT 0x10
+#define EF_ARM_PIC 0x20
+#define EF_ARM_ALIGN8 0x40 /* 8-bit structure alignment is in use */
+#define EF_ARM_NEW_ABI 0x80
+#define EF_ARM_OLD_ABI 0x100
+#define EF_ARM_SOFT_FLOAT 0x200
+#define EF_ARM_VFP_FLOAT 0x400
+#define EF_ARM_MAVERICK_FLOAT 0x800
/* Other constants defined in the ARM ELF spec. version B-01. */
/* NB. These conflict with values defined above. */
@@ -2022,13 +2026,21 @@ typedef Elf32_Addr Elf32_Conflict;
#define EF_ARM_MAPSYMSFIRST 0x10
#define EF_ARM_EABIMASK 0XFF000000
+/* Constants defined in AAELF. */
+#define EF_ARM_BE8 0x00800000
+#define EF_ARM_LE8 0x00400000
+
#define EF_ARM_EABI_VERSION(flags) ((flags) & EF_ARM_EABIMASK)
#define EF_ARM_EABI_UNKNOWN 0x00000000
#define EF_ARM_EABI_VER1 0x01000000
#define EF_ARM_EABI_VER2 0x02000000
+#define EF_ARM_EABI_VER3 0x03000000
+#define EF_ARM_EABI_VER4 0x04000000
+#define EF_ARM_EABI_VER5 0x05000000
/* Additional symbol types for Thumb */
-#define STT_ARM_TFUNC 0xd
+#define STT_ARM_TFUNC STT_LOPROC /* A Thumb function. */
+#define STT_ARM_16BIT STT_HIPROC /* A Thumb label. */
/* ARM-specific values for sh_flags */
#define SHF_ARM_ENTRYSECT 0x10000000 /* Section contains an entry point */
@@ -2038,6 +2050,17 @@ typedef Elf32_Addr Elf32_Conflict;
/* ARM-specific program header flags */
#define PF_ARM_SB 0x10000000 /* Segment contains the location
addressed by the static base */
+#define PF_ARM_PI 0x20000000 /* Position-independent segment. */
+#define PF_ARM_ABS 0x40000000 /* Absolute segment. */
+
+/* Processor specific values for the Phdr p_type field. */
+#define PT_ARM_EXIDX (PT_LOPROC + 1) /* ARM unwind segment. */
+
+/* Processor specific values for the Shdr sh_type field. */
+#define SHT_ARM_EXIDX (SHT_LOPROC + 1) /* ARM unwind section. */
+#define SHT_ARM_PREEMPTMAP (SHT_LOPROC + 2) /* Preemption details. */
+#define SHT_ARM_ATTRIBUTES (SHT_LOPROC + 3) /* ARM attributes section. */
+
/* ARM relocs. */
#define R_ARM_NONE 0 /* No reloc */
@@ -2050,7 +2073,7 @@ typedef Elf32_Addr Elf32_Conflict;
#define R_ARM_THM_ABS5 7
#define R_ARM_ABS8 8 /* Direct 8 bit */
#define R_ARM_SBREL32 9
-#define R_ARM_THM_PC22 10
+#define R_ARM_THM_CALL 10
#define R_ARM_THM_PC8 11
#define R_ARM_AMP_VCALL9 12
#define R_ARM_SWI24 13
@@ -2065,16 +2088,36 @@ typedef Elf32_Addr Elf32_Conflict;
#define R_ARM_GOTPC 25 /* 32 bit PC relative offset to GOT */
#define R_ARM_GOT32 26 /* 32 bit GOT entry */
#define R_ARM_PLT32 27 /* 32 bit PLT address */
+#define R_ARM_CALL 28
+#define R_ARM_JUMP24 29
+#define R_ARM_THM_JUMP24 30
+#define R_ARM_BASE_ABS 31
#define R_ARM_ALU_PCREL_7_0 32
#define R_ARM_ALU_PCREL_15_8 33
#define R_ARM_ALU_PCREL_23_15 34
#define R_ARM_LDR_SBREL_11_0 35
#define R_ARM_ALU_SBREL_19_12 36
#define R_ARM_ALU_SBREL_27_20 37
+#define R_ARM_TLS_GOTDESC 90
+#define R_ARM_TLS_CALL 91
+#define R_ARM_TLS_DESCSEQ 92
+#define R_ARM_THM_TLS_CALL 93
#define R_ARM_GNU_VTENTRY 100
#define R_ARM_GNU_VTINHERIT 101
#define R_ARM_THM_PC11 102 /* thumb unconditional branch */
#define R_ARM_THM_PC9 103 /* thumb conditional branch */
+#define R_ARM_TLS_GD32 104 /* PC-rel 32 bit for global dynamic
+ thread local data */
+#define R_ARM_TLS_LDM32 105 /* PC-rel 32 bit for local dynamic
+ thread local data */
+#define R_ARM_TLS_LDO32 106 /* 32 bit offset relative to TLS
+ block */
+#define R_ARM_TLS_IE32 107 /* PC-rel 32 bit for GOT entry of
+ static TLS block offset */
+#define R_ARM_TLS_LE32 108 /* 32 bit offset relative to static
+ TLS block */
+#define R_ARM_THM_TLS_DESCSEQ 129
+#define R_ARM_IRELATIVE 160
#define R_ARM_RXPC25 249
#define R_ARM_RSBREL32 250
#define R_ARM_THM_RPC22 251
diff -purN trunk/include/grub/libgcc.h arm-uboot/include/grub/libgcc.h
--- trunk/include/grub/libgcc.h 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/include/grub/libgcc.h 2012-11-20 17:11:55.042928259 +0000
@@ -120,3 +120,14 @@ void EXPORT_FUNC (_savegpr_29) (void);
void EXPORT_FUNC (_savegpr_30) (void);
void EXPORT_FUNC (_savegpr_31) (void);
#endif
+
+#if defined (__arm__)
+void EXPORT_FUNC (__aeabi_idiv) (void);
+void EXPORT_FUNC (__aeabi_idivmod) (void);
+void EXPORT_FUNC (__aeabi_lasr) (void);
+void EXPORT_FUNC (__aeabi_llsl) (void);
+void EXPORT_FUNC (__aeabi_llsr) (void);
+void EXPORT_FUNC (__aeabi_uidiv) (void);
+void EXPORT_FUNC (__aeabi_uidivmod) (void);
+void EXPORT_FUNC (__clear_cache) (void *, void *);
+#endif
diff -purN trunk/include/grub/offsets.h arm-uboot/include/grub/offsets.h
--- trunk/include/grub/offsets.h 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/include/grub/offsets.h 2012-11-20 16:26:54.950920000 +0000
@@ -102,6 +102,7 @@
#define GRUB_KERNEL_I386_IEEE1275_MOD_GAP 0x0
#define GRUB_KERNEL_I386_COREBOOT_MOD_GAP 0x0
#define GRUB_KERNEL_SPARC64_IEEE1275_MOD_GAP 0x0
+#define GRUB_KERNEL_ARM_UBOOT_MOD_GAP 0x0
#define GRUB_KERNEL_POWERPC_IEEE1275_MOD_ALIGN 0x1000
#define GRUB_KERNEL_SPARC64_IEEE1275_MOD_ALIGN 0x1
@@ -110,6 +111,10 @@
#define GRUB_KERNEL_MIPS_ARC_MOD_ALIGN 0x1
#define GRUB_KERNEL_MIPS_QEMU_MIPS_MOD_ALIGN 0x1
+#define GRUB_KERNEL_ARM_UBOOT_MOD_ALIGN 0x1
+#define GRUB_KERNEL_ARM_UBOOT_TOTAL_MODULE_SIZE 0x4
+#define GRUB_KERNEL_ARM_UBOOT_LINK_ADDR 0x08000000
+
/* Minimal gap between _end and the start of the modules. It's a hack
for PowerMac to prevent "CLAIM failed" error. The real fix is to
rewrite grub-mkimage to generate valid ELF files. */
diff -purN trunk/include/grub/symbol.h arm-uboot/include/grub/symbol.h
--- trunk/include/grub/symbol.h 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/include/grub/symbol.h 2012-11-20 16:26:54.950920000 +0000
@@ -29,12 +29,16 @@
#if HAVE_ASM_USCORE
#ifdef ASM_FILE
-# define EXT_C(sym) _ ## sym
+#ifndef (__arm__)
+#define EXT_C(sym) _ ## sym
#else
-# define EXT_C(sym) "_" sym
+#define EXT_C(sym) % ## sym
#endif
#else
-# define EXT_C(sym) sym
+#define EXT_C(sym) "_" sym
+#endif
+#else
+#define EXT_C(sym) sym
#endif
#if defined (__APPLE__)
@@ -52,8 +56,8 @@
/* Mark an exported symbol. */
#ifndef GRUB_SYMBOL_GENERATOR
-# define EXPORT_FUNC(x) x
-# define EXPORT_VAR(x) x
+#define EXPORT_FUNC(x) x
+#define EXPORT_VAR(x) x
#endif /* ! GRUB_SYMBOL_GENERATOR */
#endif /* ! GRUB_SYMBOL_HEADER */
diff -purN trunk/include/grub/uboot/api_public.h arm-uboot/include/grub/uboot/api_public.h
--- trunk/include/grub/uboot/api_public.h 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/include/grub/uboot/api_public.h 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,184 @@
+/*
+ * (C) Copyright 2007-2008 Semihalf
+ *
+ * Written by: Rafal Jaworowski <raj@semihalf.com>
+ *
+ * This file is dual licensed; you can use it under the terms of
+ * either the GPL, or the BSD license, at your option.
+ *
+ * I. GPL:
+ *
+ * This file 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.
+ *
+ * This file 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; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ * Alternatively,
+ *
+ * II. BSD license:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _API_PUBLIC_H_
+#define _API_PUBLIC_H_
+
+#define API_EINVAL 1 /* invalid argument(s) */
+#define API_ENODEV 2 /* no device */
+#define API_ENOMEM 3 /* no memory */
+#define API_EBUSY 4 /* busy, occupied etc. */
+#define API_EIO 5 /* I/O error */
+#define API_ESYSC 6 /* syscall error */
+
+typedef int (*scp_t) (int, int *, ...);
+
+typedef grub_uint16_t uint16_t;
+typedef grub_uint32_t uint32_t;
+
+#define API_SIG_VERSION 1
+#define API_SIG_MAGIC "UBootAPI"
+#define API_SIG_MAGLEN 8
+
+struct api_signature
+{
+ char magic[API_SIG_MAGLEN]; /* magic string */
+ uint16_t version; /* API version */
+ uint32_t checksum; /* checksum of this sig struct */
+ scp_t syscall; /* entry point to the API */
+};
+
+enum
+{
+ API_RSVD = 0,
+ API_GETC,
+ API_PUTC,
+ API_TSTC,
+ API_PUTS,
+ API_RESET,
+ API_GET_SYS_INFO,
+ API_UDELAY,
+ API_GET_TIMER,
+ API_DEV_ENUM,
+ API_DEV_OPEN,
+ API_DEV_CLOSE,
+ API_DEV_READ,
+ API_DEV_WRITE,
+ API_ENV_ENUM,
+ API_ENV_GET,
+ API_ENV_SET,
+ API_DISPLAY_GET_INFO,
+ API_DISPLAY_DRAW_BITMAP,
+ API_DISPLAY_CLEAR,
+ API_MAXCALL
+};
+
+#define MR_ATTR_FLASH 0x0001
+#define MR_ATTR_DRAM 0x0002
+#define MR_ATTR_SRAM 0x0003
+#define MR_ATTR_MASK 0x000f
+
+struct mem_region
+{
+ unsigned long start;
+ unsigned long size;
+ int flags;
+};
+
+struct sys_info
+{
+ unsigned long clk_bus;
+ unsigned long clk_cpu;
+ unsigned long bar;
+ struct mem_region *mr;
+ int mr_no; /* number of memory regions */
+};
+
+#undef CONFIG_SYS_64BIT_LBA
+#ifdef CONFIG_SYS_64BIT_LBA
+typedef u_int64_t lbasize_t;
+#else
+typedef unsigned long lbasize_t;
+#endif
+typedef unsigned long lbastart_t;
+
+#define DEV_TYP_NONE 0x0000
+#define DEV_TYP_NET 0x0001
+
+#define DEV_TYP_STOR 0x0002
+#define DT_STOR_IDE 0x0010
+#define DT_STOR_SCSI 0x0020
+#define DT_STOR_USB 0x0040
+#define DT_STOR_MMC 0x0080
+#define DT_STOR_SATA 0x0100
+
+#define DEV_STA_CLOSED 0x0000 /* invalid, closed */
+#define DEV_STA_OPEN 0x0001 /* open i.e. active */
+
+struct device_info
+{
+ int type;
+ void *cookie;
+
+ union
+ {
+ struct
+ {
+ lbasize_t block_count; /* no of blocks */
+ unsigned long block_size; /* size of one block */
+ } storage;
+
+ struct
+ {
+ unsigned char hwaddr[6];
+ } net;
+ } info;
+#define di_stor info.storage
+#define di_net info.net
+
+ int state;
+};
+
+#define DISPLAY_TYPE_LCD 0x0001
+#define DISPLAY_TYPE_VIDEO 0x0002
+
+struct display_info
+{
+ int type;
+ /* screen size in pixels */
+ int pixel_width;
+ int pixel_height;
+ /* screen size in rows and columns of text */
+ int screen_rows;
+ int screen_cols;
+};
+
+#endif /* _API_PUBLIC_H_ */
diff -purN trunk/include/grub/uboot/console.h arm-uboot/include/grub/uboot/console.h
--- trunk/include/grub/uboot/console.h 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/include/grub/uboot/console.h 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,29 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/>.
+ */
+
+#ifndef GRUB_CONSOLE_MACHINE_HEADER
+#define GRUB_CONSOLE_MACHINE_HEADER 1
+
+/* Initialize the console system. */
+void grub_console_init_early (void);
+void grub_console_init_lately (void);
+
+/* Exit the console system. */
+void grub_console_fini (void);
+
+#endif /* ! GRUB_CONSOLE_MACHINE_HEADER */
diff -purN trunk/include/grub/uboot/disk.h arm-uboot/include/grub/uboot/disk.h
--- trunk/include/grub/uboot/disk.h 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/include/grub/uboot/disk.h 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,44 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/>.
+ */
+
+#ifndef GRUB_UBOOT_DISK_HEADER
+#define GRUB_UBOOT_DISK_HEADER 1
+
+#include <grub/symbol.h>
+#include <grub/disk.h>
+#include <grub/uboot/uboot.h>
+
+void grub_ubootdisk_init (void);
+void grub_ubootdisk_fini (void);
+
+enum disktype
+{ cd, fd, hd };
+
+struct ubootdisk_data
+{
+ struct ubootdisk_data *next;
+ void *cookie;
+ int handle;
+ int opencount;
+ enum disktype type;
+ grub_uint32_t block_size;
+};
+
+grub_err_t grub_ubootdisk_register (struct device_info *newdev, int handle);
+
+#endif /* ! GRUB_UBOOT_DISK_HEADER */
diff -purN trunk/include/grub/uboot/uboot.h arm-uboot/include/grub/uboot/uboot.h
--- trunk/include/grub/uboot/uboot.h 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/include/grub/uboot/uboot.h 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,148 @@
+/* uboot.h - declare variables and functions for U-Boot support */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2012 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/>.
+ */
+
+#ifndef GRUB_UBOOT_UBOOT_HEADER
+#define GRUB_UBOOT_UBOOT_HEADER 1
+
+#include <grub/types.h>
+#include <grub/dl.h>
+
+/* Functions. */
+void grub_uboot_mm_init (void);
+void grub_uboot_init (void);
+void grub_uboot_fini (void);
+
+void uboot_return (int) __attribute__ ((noreturn));
+
+grub_err_t grub_uboot_probe_hardware (void);
+
+extern grub_addr_t EXPORT_VAR (start_of_ram);
+
+grub_uint32_t EXPORT_FUNC (uboot_get_machine_type) (void);
+grub_addr_t EXPORT_FUNC (uboot_get_boot_data) (void);
+
+
+/*
+ * The U-Boot API operates through a "syscall" interface, consisting of an
+ * entry point address and a set of syscall numbers. The location of this
+ * entry point is described in a structure allocated on the U-Boot heap.
+ * We scan through a defined region around the hint address passed to us
+ * from U-Boot.
+ */
+#include <grub/uboot/api_public.h>
+
+#define UBOOT_API_SEARCH_LEN (3 * 1024 * 1024)
+int uboot_api_init (void);
+
+/* All functions below are wrappers around the uboot_syscall() function */
+
+/*
+ * int API_getc(int *c)
+ */
+int uboot_getc (void);
+
+/*
+ * int API_tstc(int *c)
+ */
+int uboot_tstc (void);
+
+/*
+ * int API_putc(char *ch)
+ */
+void uboot_putc (int c);
+
+/*
+ * int API_puts(const char *s)
+ */
+void uboot_puts (const char *s);
+
+/*
+ * int API_reset(void)
+ */
+void EXPORT_FUNC (uboot_reset) (void);
+
+/*
+ * 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 API_udelay(unsigned long *udelay)
+ */
+void uboot_udelay (grub_uint32_t usec);
+
+/*
+ * int API_get_timer(unsigned long *current, unsigned long *base)
+ */
+grub_uint32_t uboot_get_timer (grub_uint32_t base);
+
+/*
+ * int API_dev_enum(struct device_info *)
+ */
+int uboot_dev_enum (void);
+
+struct device_info *uboot_dev_get (int handle);
+
+/*
+ * int API_dev_open(struct device_info *)
+ */
+int uboot_dev_open (int handle);
+
+/*
+ * int API_dev_close(struct device_info *)
+ */
+int uboot_dev_close (int handle);
+
+/*
+ * 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_write (int handle, void *buf, int *len);
+
+/*
+ * 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);
+
+int uboot_dev_recv (int handle, void *buf, int size, int *real_size);
+int uboot_dev_send (int handle, void *buf, int size);
+
+/*
+ * int API_env_get(const char *name, char **value)
+ */
+char *uboot_env_get (const char *name);
+
+/*
+ * int API_env_set(const char *name, const char *value)
+ */
+void uboot_env_set (const char *name, const char *value);
+
+#endif /* ! GRUB_UBOOT_UBOOT_HEADER */
diff -purN trunk/Makefile.util.def arm-uboot/Makefile.util.def
--- trunk/Makefile.util.def 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/Makefile.util.def 2012-11-20 16:26:54.950920000 +0000
@@ -469,6 +469,7 @@ script = {
enable = mips_loongson;
enable = ia64_efi;
enable = powerpc_ieee1275;
+ enable = arm_uboot;
};
script = {
diff -purN trunk/util/grub-install.in arm-uboot/util/grub-install.in
--- trunk/util/grub-install.in 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/util/grub-install.in 2012-11-20 16:26:54.950920000 +0000
@@ -321,6 +321,8 @@ if [ x$source_dir = x ]; then
target=i386-pc
fi
;;
+ x"arm"*)
+ target="arm-uboot";;
*)
gettext "Unable to determine your platform. Use --target." ;
echo ;;
@@ -340,7 +342,7 @@ if [ "${grub_modinfo_target_cpu}-${grub_
if [ x$disk_module = xunspecified ]; then
disk_module=biosdisk
fi
-elif [ "${grub_modinfo_platform}" = "ieee1275" ] || [ "${grub_modinfo_platform}" = "efi" ] || [ "${grub_modinfo_platform}" = "arc" ] ; then
+elif [ "${grub_modinfo_platform}" = "ieee1275" ] || [ "${grub_modinfo_platform}" = "efi" ] || [ "${grub_modinfo_platform}" = "arc" ] || [ "${grub_modinfo_platform}" = "uboot" ] ; then
disk_module=
else
disk_module=native
@@ -858,6 +860,14 @@ elif [ x"$grub_modinfo_platform" = xefi
-L "$bootloader_id" -l "\\EFI\\$efi_distributor\\$efi_file"
fi
fi
+elif [ x"${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = xarm-uboot ]; then
+ grub_imgname="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}"
+ raw_imgname="${uboot_imgname}.raw"
+ mv "$grub_imgname" "$raw_imgname"
+ mkimage -T kernel -A ARM -O Linux -a 0x08000000 -e 0x08000000 -C none -d "$raw_imgname" "$grub_imgname"
+ if [ $? -eq 0 ]; then
+ rm -f "$raw_imgname"
+ fi
else
gettext "WARNING: no platform-specific install was performed" 1>&2
echo 1>&2
diff -purN trunk/util/grub-mkimage.c arm-uboot/util/grub-mkimage.c
--- trunk/util/grub-mkimage.c 2012-11-20 16:21:44.863032000 +0000
+++ arm-uboot/util/grub-mkimage.c 2012-11-20 16:26:54.950920000 +0000
@@ -69,7 +69,7 @@ struct image_target_desc
IMAGE_SPARC64_AOUT, IMAGE_SPARC64_RAW, IMAGE_I386_IEEE1275,
IMAGE_LOONGSON_ELF, IMAGE_QEMU, IMAGE_PPC, IMAGE_YEELOONG_FLASH,
IMAGE_FULOONG2F_FLASH, IMAGE_I386_PC_PXE, IMAGE_MIPS_ARC,
- IMAGE_QEMU_MIPS_FLASH
+ IMAGE_QEMU_MIPS_FLASH, IMAGE_UBOOT,
} id;
enum
{
@@ -453,6 +453,25 @@ struct image_target_desc image_targets[]
.link_align = GRUB_KERNEL_MIPS_QEMU_MIPS_LINK_ALIGN,
.default_compression = COMPRESSION_NONE
},
+ {
+ .dirname = "arm-uboot",
+ .names = { "arm-uboot", NULL },
+ .voidp_sizeof = 4,
+ .bigendian = 0,
+ .id = IMAGE_UBOOT,
+ .flags = PLATFORM_FLAGS_NONE,
+ .total_module_size = GRUB_KERNEL_ARM_UBOOT_TOTAL_MODULE_SIZE,
+ .decompressor_compressed_size = TARGET_NO_FIELD,
+ .decompressor_uncompressed_size = TARGET_NO_FIELD,
+ .decompressor_uncompressed_addr = TARGET_NO_FIELD,
+ .section_align = 1,
+ .vaddr_offset = 0,
+ .link_addr = GRUB_KERNEL_ARM_UBOOT_LINK_ADDR,
+ .elf_target = EM_ARM,
+ .mod_gap = GRUB_KERNEL_ARM_UBOOT_MOD_GAP,
+ .mod_align = GRUB_KERNEL_ARM_UBOOT_MOD_ALIGN,
+ .link_align = 4
+ },
};
#define grub_target_to_host32(x) (grub_target_to_host32_real (image_target, (x)))
@@ -1626,6 +1645,9 @@ generate_image (const char *dir, const c
core_size = program_size + header_size + footer_size;
}
break;
+ case IMAGE_UBOOT:
+ /* Raw image, header added by grub-install */
+ break;
}
grub_util_write_image (core_img, core_size, out, outname);
@@ -1862,7 +1884,6 @@ main (int argc, char *argv[])
if (! fp)
grub_util_error (_("cannot open `%s': %s"), arguments.output,
strerror (errno));
- free (arguments.output);
}
if (!arguments.dir)
@@ -1889,5 +1910,8 @@ main (int argc, char *argv[])
if (arguments.dir)
free (arguments.dir);
+ if (arguments.output)
+ free (arguments.output);
+
return 0;
}
diff -purN trunk/util/import_libfdt.py arm-uboot/util/import_libfdt.py
--- trunk/util/import_libfdt.py 1970-01-01 01:00:00.000000000 +0100
+++ arm-uboot/util/import_libfdt.py 2012-11-20 16:26:54.950920000 +0000
@@ -0,0 +1,103 @@
+#*
+#* GRUB -- GRand Unified Bootloader
+#* Copyright (C) 2012 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/>.
+#*
+
+import re
+import sys
+import os
+import codecs
+import datetime
+
+if len (sys.argv) < 3:
+ print ("Usage: %s SOURCE DESTINATION" % sys.argv[0])
+ exit (0)
+dtcdir = sys.argv[1]
+indir = os.path.join (dtcdir, "libfdt/")
+outdir = os.path.join (sys.argv[2], "lib/dtc-grub/libfdt/")
+try:
+ os.makedirs (outdir)
+except:
+ print ("WARNING: %s already exists" % outdir)
+
+conf = codecs.open (os.path.join ("grub-core/", "Makefile.libfdt.def"), "w", "utf-8")
+conf.write ("AutoGen definitions Makefile.tpl;\n\n")
+conf.write ("library = {\n")
+conf.write (" name = libfdtlib.a;\n")
+conf.write (" common = lib/dtc-grub/libfdt/fdt.c;\n")
+conf.write (" common = lib/dtc-grub/libfdt/fdt_ro.c;\n")
+conf.write (" common = lib/dtc-grub/libfdt/fdt_rw.c;\n")
+conf.write (" common = lib/dtc-grub/libfdt/fdt_strerror.c;\n")
+conf.write (" common = lib/dtc-grub/libfdt/fdt_sw.c;\n")
+conf.write (" common = lib/dtc-grub/libfdt/fdt_wip.c;\n")
+conf.write (" cppflags = '$(CPPFLAGS_LIBFDT)';\n")
+conf.write ("\n")
+conf.write (" enable = fdt;\n")
+conf.write ("};\n")
+
+conf.close ();
+
+
+libfdt_files = sorted (os.listdir (indir))
+chlog = ""
+
+for libfdt_file in libfdt_files:
+ infile = os.path.join (indir, (libfdt_file))
+ outfile = os.path.join (outdir, (libfdt_file))
+
+ if not re.match (".*\.[ch]$", libfdt_file):
+ chlog = "%s * %s: Removed\n" % (chlog, libfdt_file)
+ continue
+
+# print ("file: %s, infile: %s, outfile: %s" % (libfdt_file, infile, outfile))
+
+ f = codecs.open (infile, "r", "utf-8")
+ fw = codecs.open (outfile, "w", "utf-8")
+
+ lineno = 1
+
+ fw.write ("/* This file was automatically imported with \n")
+ fw.write (" import_libfdt.py. Please don't modify it */\n")
+ fw.write ("#include <grub/dl.h>\n")
+
+ # libfdt is dual-licensed: BSD or GPLv2+
+ if re.match (".*\.c$", libfdt_file):
+ fw.write ("GRUB_MOD_LICENSE (\"GPLv2+\");\n")
+
+ lines = f.readlines()
+
+ for line in lines:
+ fw.write (line)
+
+ f.close ()
+ fw.close ()
+
+patchfile = os.path.join (dtcdir, "libfdt-grub.diff")
+#print "Patchfile: %s\n" % patchfile
+ret = os.system("patch -d %s -p1 < %s" % (outdir, patchfile))
+if ret:
+ chlog = "%s * Applied Grub build patch\n" % chlog
+
+
+dt = datetime.date.today ()
+fw = codecs.open (os.path.join (outdir, "ImportLog"), "w", "utf-8")
+fw.write ("%04d-%02d-%02d Automatic import tool\n" % \
+ (dt.year,dt.month, dt.day))
+fw.write ("\n")
+fw.write (" Imported libfdt to GRUB\n")
+fw.write ("\n")
+fw.write (chlog)
+fw.close ()
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2012-11-20 18:19 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-20 18:19 [RFCv2] GRUB port for ARMv7-A U-Boot Leif Lindholm
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.