Skip to contentSkip to conversation

iterate return values

DISCUSSION Hollis Blanchard 3 participants 5 messages Jan 23, 2005

lore
Share & tools
lore
#0 lore raw

This patch fixed it for me, tested on Apple and DOS partition maps.

(The #if 0 stuff is an example of what I'm thinking about for grub_debug_printf, btw, I will remove it from this patch before checking in.)

Is this OK?

-Hollis

2005-01-23 Hollis Blanchard <[email protected]>

* include/grub/partition.h (grub_partition_map): Remove trailing whitespace. Add present' function pointer. (grub_partition_map_iterate): Add disk' argument.
* kern/partition.c (grub_partition_map_iterate): Likewise. Update all callers. Call hook' only if present' returns true.
* partmap/amiga.c (amiga_partition_map_present): New function. (grub_amiga_partition_map): Initialize `present'.
* partmap/apple.c (apple_partition_map_present): New function. (grub_apple_partition_map): Initialize `present'.
* partmap/pc.c (pc_partition_map_present): New function. (grub_pc_partition_map): Initialize `present'.

Index: include/grub/partition.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/partition.h,v retrieving revision 1.1

grub/partition.h +9 -5
diff -u -p -r1.1 partition.h
@@ -31,18 +31,21 @@ struct grub_partition_map
 {
   /* The name of the partition map type.  */
   const char *name;
-  
+
+  /* Is this partition map type present on `disk'? */
+  grub_err_t (*present) (struct grub_disk *disk);
+
   /* Call HOOK with each partition, until HOOK returns non-zero.  */
   grub_err_t (*iterate) (struct grub_disk *disk,
 			 int (*hook) (const grub_partition_t partition));
-  
+
   /* Return the partition named STR on the disk DISK.  */
   grub_partition_t (*probe) (struct grub_disk *disk,
 			     const char *str);
-  
+
   /* Return the name of the partition PARTITION.  */
   char *(*get_name) (const grub_partition_t partition);
-  
+
   /* The next partition map type.  */
   struct grub_partition_map *next;
 };
@@ -76,7 +79,8 @@ grub_err_t EXPORT_FUNC(grub_partition_it
 						int (*hook) (const grub_partition_t partition));
 char *EXPORT_FUNC(grub_partition_get_name) (const grub_partition_t partition);
 
-void EXPORT_FUNC(grub_partition_map_iterate) (int (*hook) (const grub_partition_map_t partmap));
+void EXPORT_FUNC(grub_partition_map_iterate) (struct grub_disk *disk,
+					      int (*hook) (const grub_partition_map_t partmap));
 					      
 void EXPORT_FUNC(grub_partition_map_register) (grub_partition_map_t partmap);
 

Index: kern/partition.c
===================================================================
RCS file: /cvsroot/grub/grub2/kern/partition.c,v
retrieving revision 1.1

partition.c +18 -4
diff -u -p -r1.1 partition.c
@@ -43,13 +43,27 @@ grub_partition_map_unregister (grub_part
 }
 
 void
-grub_partition_map_iterate (int (*hook) (const grub_partition_map_t partmap))
+grub_partition_map_iterate (struct grub_disk *disk,
+			    int (*hook) (const grub_partition_map_t partmap))
 {
   grub_partition_map_t p;
 
   for (p = grub_partition_map_list; p; p = p->next)
-    if (hook (p))
+  {
+    int present;
+
+    grub_errno = 0;
+    present = p->present (disk);
+
+#if 0
+    grub_printf ("%s: %s returned %d\n", __FUNCTION__, p->name, present);
+    if (! present)
+      grub_print_error ();
+#endif
+
+    if (present && hook (p))
       break;
+  }
 }
 
 grub_partition_t
@@ -68,7 +82,7 @@ grub_partition_probe (struct grub_disk *
     }
 
   /* Use the first partition map type found.  */
-  grub_partition_map_iterate (part_map_probe);
+  grub_partition_map_iterate (disk, part_map_probe);
   
   return part;
 }
@@ -84,7 +98,7 @@ grub_partition_iterate (struct grub_disk
       return partmap->iterate (disk, hook);
     }
   
-  grub_partition_map_iterate (part_map_iterate);
+  grub_partition_map_iterate (disk, part_map_iterate);
   return grub_errno;
 }
 

Index: partmap/amiga.c
===================================================================
RCS file: /cvsroot/grub/grub2/partmap/amiga.c,v
retrieving revision 1.1

amiga.c +30 -0
diff -u -p -r1.1 amiga.c
@@ -72,6 +72,35 @@ static struct grub_partition_map grub_am
 static grub_dl_t my_mod;
 #endif
 
+
+static grub_err_t
+amiga_partition_map_present (grub_disk_t disk)
+{
+  struct grub_amiga_rdsk rdsk;
+  struct grub_disk raw;
+  int pos;
+
+  /* Enforce raw disk access.  */
+  raw = *disk;
+  raw.partition = 0;
+
+  /* The RDSK block is one of the first 15 blocks.  */
+  for (pos = 0; pos < 15; pos++)
+    {
+      /* Read the RDSK block which is a descriptor for the entire disk.  */
+      if (grub_disk_read (&raw, pos, 0, sizeof (rdsk),  (char *) &rdsk))
+	goto finish;
+      
+      if (!grub_strcmp (rdsk.magic, "RDSK"))
+	break;
+    }
+
+  grub_error (GRUB_ERR_BAD_PART_TABLE, "no Amiga magic");
+
+finish:
+  return ! grub_errno;
+}
+
 static grub_err_t
 amiga_partition_map_iterate (grub_disk_t disk,
 			     int (*hook) (const grub_partition_t partition))
@@ -198,6 +227,7 @@ amiga_partition_map_get_name (const grub
 static struct grub_partition_map grub_amiga_partition_map =
   {
     .name = "amiga_partition_map",
+    .present = amiga_partition_map_present,
     .iterate = amiga_partition_map_iterate,
     .probe = amiga_partition_map_probe,
     .get_name = amiga_partition_map_get_name

Index: partmap/apple.c
===================================================================
RCS file: /cvsroot/grub/grub2/partmap/apple.c,v
retrieving revision 1.1

apple.c +22 -0
diff -u -p -r1.1 apple.c
@@ -94,6 +94,27 @@ static grub_dl_t my_mod;
 
 
 static grub_err_t
+apple_partition_map_present (grub_disk_t disk)
+{
+  struct grub_apple_part apart;
+  struct grub_disk raw;
+
+  /* Enforce raw disk access.  */
+  raw = *disk;
+  raw.partition = 0;
+
+  if (grub_disk_read (&raw, 1, 0, sizeof (struct grub_apple_part),
+		      (char *) &apart))
+    goto finish;
+
+  if (apart.magic != GRUB_APPLE_PART_MAGIC)
+    grub_error (GRUB_ERR_BAD_PART_TABLE, "missing Apple magic");
+
+finish:
+  return ! grub_errno;
+}
+
+static grub_err_t
 apple_partition_map_iterate (grub_disk_t disk,
 			int (*hook) (const grub_partition_t partition))
 {
@@ -200,6 +221,7 @@ apple_partition_map_get_name (const grub
 static struct grub_partition_map grub_apple_partition_map =
   {
     .name = "apple_partition_map",
+    .present = apple_partition_map_present,
     .iterate = apple_partition_map_iterate,
     .probe = apple_partition_map_probe,
     .get_name = apple_partition_map_get_name

Index: partmap/pc.c
===================================================================
RCS file: /cvsroot/grub/grub2/partmap/pc.c,v
retrieving revision 1.1

pc.c +23 -0
diff -u -p -r1.1 pc.c
@@ -92,6 +92,28 @@ grub_partition_parse (const char *str)
 }
 
 static grub_err_t
+pc_partition_map_present (grub_disk_t disk)
+{
+  struct grub_disk raw;
+  struct grub_pc_partition_mbr mbr;
+
+  /* Enforce raw disk access.  */
+  raw = *disk;
+  raw.partition = 0;
+
+  /* Read the MBR.  */
+  if (grub_disk_read (&raw, 0, 0, sizeof (mbr), (char *) &mbr))
+    goto finish;
+
+  /* Check if it is valid.  */
+  if (mbr.signature != grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE))
+    grub_error (GRUB_ERR_BAD_PART_TABLE, "missing DOS signature");
+
+finish:
+  return ! grub_errno;
+}
+
+static grub_err_t
 pc_partition_map_iterate (grub_disk_t disk,
 			  int (*hook) (const grub_partition_t partition))
 {
@@ -283,6 +305,7 @@ pc_partition_map_get_name (const grub_pa
 static struct grub_partition_map grub_pc_partition_map =
   {
     .name = "pc_partition_map",
+    .present = pc_partition_map_present,
     .iterate = pc_partition_map_iterate,
     .probe = pc_partition_map_probe,
     .get_name = pc_partition_map_get_name
#1 lore raw
Quoted 1 line · Hollis Blanchard

This patch fixed it for me, tested on Apple and DOS partition maps.

I vote for not adding an additional function. I think it is simpler to check an error rather than calling "present" functions. I don't want to bloat the API.

Okuji

#2 lore raw
Quoted 6 lines · Yoshinori K. Okuji
Quoted 1 line · Hollis Blanchard

This patch fixed it for me, tested on Apple and DOS partition maps.

I vote for not adding an additional function. I think it is simpler to check an error rather than calling "present" functions. I don't want to bloat the API.

I think "is this partition type present" is a very reasonable API to support...

What is the alternative? How would you distinguish between an iterate error (like "wrong partition map type") and a hook returning 1?

-Hollis

#3 lore raw
Quoted 9 lines · Hollis Blanchard
Quoted 6 lines · Yoshinori K. Okuji
Quoted 1 line · Hollis Blanchard

This patch fixed it for me, tested on Apple and DOS partition maps.

I vote for not adding an additional function. I think it is simpler to check an error rather than calling "present" functions. I don't want to bloat the API.

Quoted 5 lines

I think "is this partition type present" is a very reasonable API to support...

What is the alternative? How would you distinguish between an iterate error (like "wrong partition map type") and a hook returning 1?

You could check for the specific error (GRUB_ERR_INCORRECT_PART or so).

--
Marco

#4 lore raw
In reply to Hollis Blanchard
Quoted 2 lines · Hollis Blanchard

I think "is this partition type present" is a very reasonable API to support...

Then, the filesystem also must have a function for "if this file is present". This is overkill.

Quoted 2 lines

What is the alternative? How would you distinguish between an iterate error (like "wrong partition map type") and a hook returning 1?

So I said that iterate functions should not return non-zero on errors. They should return zero instead. Then, you can check if it was because of an error or not.

Okuji