Skip to contentSkip to conversation

U-Boot namespace and device enumeration fixes

PATCH New Leif Lindholm 1 participant 1 message May 2, 2013 Networking

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 U-Boot namespace and device enumeration fixes

This patch against the arm branch (on top of yesterday's patch) addresses the following issues:

- Changes all globally visible uboot_* to grub_uboot_*.
- Device enumeration now uses a dynamically allocated array, and all device functions now return/accept pointers rather than handles.
- Disk device initialisation now also allocates dynamically sized arrays.

/
Leif

=== modified file 'grub-core/disk/uboot/ubootdisk.c'

disk/uboot/ubootdisk.c +38 -85
@@ -26,10 +26,11 @@
 #include <grub/types.h>
 #include <grub/uboot/disk.h>
 #include <grub/uboot/uboot.h>
+#include <grub/uboot/api_public.h>
 
-static struct ubootdisk_data *fd_devices;
 static struct ubootdisk_data *hd_devices;
-static struct ubootdisk_data *cd_devices;
+static int hd_num;
+static int hd_max;
 
 /*
  * grub_ubootdisk_register():
@@ -37,73 +38,53 @@
  *   code.
  */
 grub_err_t
-grub_ubootdisk_register (struct device_info *newdev, int handle)
+grub_ubootdisk_register (struct device_info *newdev)
 {
   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;
+      /* hd */
+      if (hd_num == hd_max)
+	{
+	  int new_num;
+	  new_num = (hd_max ? hd_max * 2 : 1);
+	  d = grub_realloc(hd_devices,
+			   sizeof (struct ubootdisk_data) * new_num);
+	  if (!d)
+	    return grub_errno;
+	  hd_devices = d;
+	  hd_max = new_num;
+	}
+
+      d = &hd_devices[hd_num];
+      hd_num++;
       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->dev = newdev;
   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.
+ *   Iterator over enumerated disk devices.
  */
 static int
 uboot_disk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
 		    grub_disk_pull_t pull)
 {
-  struct ubootdisk_data *d;
   char buf[16];
   int count;
 
@@ -111,7 +92,7 @@
     {
     case GRUB_DISK_PULL_NONE:
       /* "hd" - built-in mass-storage */
-      for (d = hd_devices, count = 0; d; d = d->next, count++)
+      for (count = 0 ; count < hd_num; count++)
 	{
 	  grub_snprintf (buf, sizeof (buf) - 1, "hd%d", count);
 	  grub_dprintf ("ubootdisk", "iterating %s\n", buf);
@@ -119,25 +100,6 @@
 	    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, hook_data))
-	    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, hook_data))
-	    return 1;
-	}
-      break;
     default:
       return 0;
     }
@@ -147,15 +109,10 @@
 
 /* Helper function for uboot_disk_open. */
 static struct ubootdisk_data *
-get_device (struct ubootdisk_data *devices, int num)
+get_hd_device (int num)
 {
-  struct ubootdisk_data *d;
-
-  for (d = devices; d && num; d = d->next, num--)
-    ;
-
-  if (num == 0)
-    return d;
+  if (num < hd_num)
+    return &hd_devices[num];
 
   return NULL;
 }
@@ -190,14 +147,8 @@
 
   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);
+      d = get_hd_device (num);
       break;
     default:
       goto fail;
@@ -219,7 +170,7 @@
     }
   else
     {
-      retval = uboot_dev_open (d->handle);
+      retval = grub_uboot_dev_open (d->dev);
       if (retval != 0)
 	goto fail;
       d->opencount = 1;
@@ -228,9 +179,7 @@
   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;
+  devinfo = d->dev;
 
   d->block_size = devinfo->di_stor.block_size;
   if (d->block_size == 0)
@@ -246,7 +195,11 @@
   grub_dprintf ("ubootdisk", "(%s) blocksize=%d, log_sector_size=%d\n",
 		disk->name, d->block_size, disk->log_sector_size);
 
-  disk->total_sectors = devinfo->di_stor.block_count ? : GRUB_DISK_SIZE_UNKNOWN;
+  if (devinfo->di_stor.block_count)
+    disk->total_sectors = devinfo->di_stor.block_count;
+  else
+    disk->total_sectors = GRUB_DISK_SIZE_UNKNOWN;
+
   disk->data = d;
 
   return GRUB_ERR_NONE;
@@ -275,7 +228,7 @@
     }
   else if (d->opencount == 1)
     {
-      retval = uboot_dev_close (d->handle);
+      retval = grub_uboot_dev_close (d->dev);
       d->opencount--;
       grub_dprintf ("ubootdisk", "closed %s (%d)\n", disk->name, retval);
     }
@@ -296,12 +249,12 @@
 		 grub_disk_addr_t offset, grub_size_t numblocks, char *buf)
 {
   struct ubootdisk_data *d;
-  lbasize_t real_size;
+  grub_size_t real_size;
   int retval;
 
   d = disk->data;
 
-  retval = uboot_dev_read (d->handle, buf, numblocks, offset, &real_size);
+  retval = grub_uboot_dev_read (d->dev, 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,
@@ -318,8 +271,8 @@
 		  grub_size_t size __attribute__ ((unused)),
 		  const char *buf __attribute__ ((unused)))
 {
-  grub_dprintf ("ubootdisk", "attempt to write\n");
-  return GRUB_ERR_NOT_IMPLEMENTED_YET;
+  return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+		     "attempt to write (not supported)");
 }
 
 static struct grub_disk_dev grub_ubootdisk_dev = {

=== modified file 'grub-core/kern/arm/uboot/startup.S'

kern/arm/uboot/startup.S +10 -10
@@ -65,15 +65,15 @@
 	ldr	sp, =entry_state
 	push	{r4-r12,lr}	@ store U-Boot context (sp in r12)
 
-	ldr     r12, =EXT_C(uboot_machine_type)
+	ldr     r12, =EXT_C(grub_uboot_machine_type)
 	str     r1, [r12]
-	ldr     r12, =EXT_C(uboot_boot_data)
+	ldr     r12, =EXT_C(grub_uboot_boot_data)
 	str     r2, [r12]
 
 	@ 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.
-	bl	uboot_get_real_bss_start	@ r0 = src
+	bl	grub_uboot_get_real_bss_start	@ r0 = src
 	ldr	r1, =EXT_C(_end)		@ dst = End of BSS
 	ldr	r2, grub_total_module_size	@ blob size
 	add	r3, r0, r2			@ blob end
@@ -92,7 +92,7 @@
 
 	@ Since we _are_ the C run-time, we need to manually zero the BSS
 	@ region before continuing
-	bl	uboot_get_real_bss_start	@ zero from here
+	bl	grub_uboot_get_real_bss_start	@ zero from here
 	ldr	r1, =EXT_C(_end)		@ to here
 	mov	r2, #0
 1:	str	r2, [r0], #4
@@ -105,7 +105,7 @@
 	 * __bss_start does not actually point to the start of the runtime
 	 * BSS, but rather to the next byte following the preceding data.
 	 */
-FUNCTION (uboot_get_real_bss_start)
+FUNCTION (grub_uboot_get_real_bss_start)
 	ldr	r0, =EXT_C(__bss_start)		@ src
 	tst	r0, #(GRUB_KERNEL_MACHINE_MOD_ALIGN - 1)
 	beq	1f
@@ -121,12 +121,12 @@
 	 *   r12 (ip). Furthermore it needs to restore r8 for
 	 *   U-Boot (Global Data Pointer) and preserve it for Grub.
 	 */
-FUNCTION(uboot_syscall)
+FUNCTION(grub_uboot_syscall)
 	ldr	ip, =transition_space
 	stm	ip, {r8, lr}
 	ldr	ip, =gd_backup
 	ldr	r8, [ip]
-	ldr	ip, =uboot_syscall_ptr
+	ldr	ip, =grub_uboot_syscall_ptr
 	mov	lr, pc
 	ldr	pc, [ip]
 	ldr	ip, =gd_backup
@@ -135,7 +135,7 @@
 	ldm	ip, {r8, lr}
 	bx	lr
 	
-FUNCTION(uboot_return)
+FUNCTION(grub_uboot_return)
 	ldr	sp, =entry_state_end
 	pop	{r4-r12, lr}
 	mov	sp, r12
@@ -155,7 +155,7 @@
 	.long	0	@ r9
 	.long	0	@ r10
 	.long	0	@ r11
-VARIABLE(uboot_search_hint)@ U-Boot stack pointer - 
+VARIABLE(grub_uboot_search_hint)@ U-Boot stack pointer - 
 	.long	0	@ also API signature address hint.
 	.long	0	@ lr
 entry_state:		@ backup for U-Boot context
@@ -165,7 +165,7 @@
 	.long	0	@ r8
 	.long	0	@ lr
 
-VARIABLE(uboot_syscall_ptr)
+VARIABLE(grub_uboot_syscall_ptr)
 	.long	0	@
 
 	.end

=== modified file 'grub-core/kern/uboot/hw.c'

kern/uboot/hw.c +8 -7
@@ -25,6 +25,7 @@
 #include <grub/machine/kernel.h>
 #include <grub/uboot/disk.h>
 #include <grub/uboot/uboot.h>
+#include <grub/uboot/api_public.h>
 
 grub_addr_t start_of_ram;
 
@@ -37,7 +38,7 @@
 void
 grub_uboot_mm_init (void)
 {
-  struct sys_info *si = uboot_get_sys_info ();
+  struct sys_info *si = grub_uboot_get_sys_info ();
 
   grub_mm_init_region ((void *) (grub_modules_get_end ()
 				 + GRUB_KERNEL_MACHINE_STACK_SIZE),
@@ -57,19 +58,18 @@
 
 /*
  * grub_uboot_probe_hardware():
- *   
  */
 grub_err_t
 grub_uboot_probe_hardware (void)
 {
   int devcount, i;
 
-  devcount = uboot_dev_enum ();
+  devcount = grub_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);
+      struct device_info *devinfo = grub_uboot_dev_get (i);
 
       grub_dprintf ("init", "device handle: %d\n", i);
       grub_dprintf ("init", "  cookie\t= 0x%08x\n",
@@ -78,11 +78,12 @@
       if (devinfo->type & DEV_TYP_STOR)
 	{
 	  grub_dprintf ("init", "  type\t\t= DISK\n");
-	  grub_ubootdisk_register (devinfo, i);
+	  grub_ubootdisk_register (devinfo);
 	}
       else if (devinfo->type & DEV_TYP_NET)
 	{
-	  grub_dprintf ("init", "  type\t\t= NET\n");
+	  /* Dealt with in ubootnet module. */
+	  grub_dprintf ("init", "  type\t\t= NET (not supported yet)\n");
 	}
       else
 	{
@@ -97,7 +98,7 @@
 grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
 {
   int i;
-  struct sys_info *si = uboot_get_sys_info ();
+  struct sys_info *si = grub_uboot_get_sys_info ();
 
   if (!si || (si->mr_no < 1))
     return GRUB_ERR_BUG;

=== modified file 'grub-core/kern/uboot/init.c'

kern/uboot/init.c +16 -15
@@ -29,41 +29,42 @@
 #include <grub/uboot/console.h>
 #include <grub/uboot/disk.h>
 #include <grub/uboot/uboot.h>
+#include <grub/uboot/api_public.h>
 
 extern char __bss_start[];
 extern char _end[];
 extern grub_size_t grub_total_module_size;
-extern int (*uboot_syscall_ptr) (int, int *, ...);
+extern int (*grub_uboot_syscall_ptr) (int, int *, ...);
 
 /* Set to anything other than zero so it lands in .data and not .bss.  */
 grub_addr_t grub_modbase = 0x55aa55aa;
-grub_uint32_t uboot_machine_type = 0x55aa55aa;
-grub_addr_t uboot_boot_data = 0x55aa55aa;
+grub_uint32_t grub_uboot_machine_type = 0x55aa55aa;
+grub_addr_t grub_uboot_boot_data = 0x55aa55aa;
 
 static unsigned long timer_start;
 
 void
 grub_exit (void)
 {
-  uboot_return (0);
+  grub_uboot_return (0);
 }
 
 grub_uint32_t
-uboot_get_machine_type (void)
+grub_uboot_get_machine_type (void)
 {
-  return uboot_machine_type;
+  return grub_uboot_machine_type;
 }
 
 grub_addr_t
-uboot_get_boot_data (void)
+grub_uboot_get_boot_data (void)
 {
-  return uboot_boot_data;
+  return grub_uboot_boot_data;
 }
 
 static grub_uint64_t
 uboot_timer_ms (void)
 {
-  return (grub_uint64_t) uboot_get_timer (timer_start) / 1000;
+  return (grub_uint64_t) grub_uboot_get_timer (timer_start) / 1000;
 }
 
 void
@@ -73,7 +74,7 @@
   int ver;
 
   /* First of all - establish connection with U-Boot */
-  ver = uboot_api_init ();
+  ver = grub_uboot_api_init ();
   if (!ver)
     {
       /* Don't even have a console to log errors to... */
@@ -82,14 +83,14 @@
   else if (ver > API_SIG_VERSION)
     {
       /* Try to print an error message */
-      uboot_puts ("invalid U-Boot API version\n");
+      grub_uboot_puts ("invalid U-Boot API version\n");
     }
 
   /*
    * Modules were relocated to _end, or __bss_start + grub_total_module_size,
    * whichever greater. (And __bss_start may not point to actual BSS start...)
    */
-  real_bss_start = uboot_get_real_bss_start ();
+  real_bss_start = grub_uboot_get_real_bss_start ();
   end = real_bss_start + grub_total_module_size;
   if (end < (grub_addr_t) _end)
     end = (grub_addr_t) _end;
@@ -116,7 +117,7 @@
   grub_uboot_probe_hardware ();
 
   /* Initialise timer */
-  timer_start = uboot_get_timer (0);
+  timer_start = grub_uboot_get_timer (0);
   grub_install_get_time_ms (uboot_timer_ms);
 
   /* Initialize  */
@@ -140,7 +141,7 @@
 {
   char *tmp;
 
-  tmp = uboot_env_get ("grub_bootdev");
+  tmp = grub_uboot_env_get ("grub_bootdev");
   if (tmp)
     {
       *device = grub_strdup (tmp);
@@ -150,7 +151,7 @@
   else
     *device = NULL;
 
-  tmp = uboot_env_get ("grub_bootpath");
+  tmp = grub_uboot_env_get ("grub_bootpath");
   if (tmp)
     {
       *path = grub_strdup (tmp);

=== modified file 'grub-core/kern/uboot/uboot.c'

kern/uboot/uboot.c +188 -225

507 lines not on this page · raw message

=== modified file 'grub-core/lib/uboot/reboot.c'

lib/uboot/reboot.c +1 -1
@@ -25,6 +25,6 @@
 {
   grub_machine_fini ();
 
-  uboot_reset ();
+  grub_uboot_reset ();
   while (1);
 }

=== modified file 'grub-core/net/drivers/uboot/ubootnet.c'

net/drivers/uboot/ubootnet.c +9 -32

119 lines not on this page · raw message

=== modified file 'grub-core/term/uboot/console.c'

term/uboot/console.c +4 -4
@@ -28,14 +28,14 @@
 static void
 put (struct grub_term_output *term __attribute__ ((unused)), const int c)
 {
-  uboot_putc (c);
+  grub_uboot_putc (c);
 }
 
 static int
 readkey (struct grub_term_input *term __attribute__ ((unused)))
 {
-  if (uboot_tstc () > 0)
-    return uboot_getc ();
+  if (grub_uboot_tstc () > 0)
+    return grub_uboot_getc ();
 
   return -1;
 }
@@ -127,7 +127,7 @@
   const char *type;
 
   /* See if explicitly set by U-Boot environment */
-  type = uboot_env_get ("grub_term");
+  type = grub_uboot_env_get ("grub_term");
   if (!type)
     type = "vt100";
 

=== modified file 'include/grub/arm/linux.h'

grub/arm/linux.h +2 -2
@@ -30,8 +30,8 @@
 # define LINUX_ADDRESS        (start_of_ram + 0x8000)
 # define LINUX_INITRD_ADDRESS (start_of_ram + 0x02000000)
 # define LINUX_FDT_ADDRESS    (LINUX_INITRD_ADDRESS - 0x10000)
-# define firmware_get_boot_data uboot_get_boot_data
-# define firmware_get_machine_type uboot_get_machine_type
+# define firmware_get_boot_data grub_uboot_get_boot_data
+# define firmware_get_machine_type grub_uboot_get_machine_type
 #elif defined GRUB_MACHINE_EFI
 # include <grub/efi/efi.h>
 # include <grub/machine/loader.h>

=== modified file 'include/grub/uboot/disk.h'

grub/uboot/disk.h +2 -3
@@ -31,14 +31,13 @@
 
 struct ubootdisk_data
 {
-  struct ubootdisk_data *next;
   void *cookie;
-  int handle;
+  struct device_info *dev;
   int opencount;
   enum disktype type;
   grub_uint32_t block_size;
 };
 
-grub_err_t grub_ubootdisk_register (struct device_info *newdev, int handle);
+grub_err_t grub_ubootdisk_register (struct device_info *newdev);
 
 #endif /* ! GRUB_UBOOT_DISK_HEADER */

=== modified file 'include/grub/uboot/uboot.h'

grub/uboot/uboot.h +37 -101

157 lines not on this page · raw message