]> git.ozlabs.org Git - yaboot.git/blobdiff - second/partition.c
Double block_buffer
[yaboot.git] / second / partition.c
index 4381770fcca5facc0171fc52308b273478235608..1ade1b671f4e422b28546407ecfc5aace2a2ebe9 100644 (file)
@@ -1,6 +1,8 @@
 /*
  *  partition.c - partition table support
  *
+ *  Copyright (C) 2013 Dinar Valeev
+ *
  *  Copyright (C) 2004 Sven Luther
  *
  *  Copyright (C) 2001, 2002 Ethan Benson
@@ -34,6 +36,7 @@
 #include "mac-part.h"
 #include "fdisk-part.h"
 #include "amiga-part.h"
+#include "gpt-part.h"
 #include "partition.h"
 #include "prom.h"
 #include "string.h"
@@ -60,12 +63,12 @@ static const char *valid_mac_partition_types[] = {
 #endif
 
 
-#define MAX_BLOCK_SIZE 2048
-static unsigned char block_buffer[MAX_BLOCK_SIZE];
+#define MAX_BLOCK_SIZE 4096
+static unsigned char block_buffer[MAX_BLOCK_SIZE * 2];
 
 static void
 add_new_partition(struct partition_t** list, int part_number, const char *part_type,
-                 const char *part_name, unsigned long part_start, unsigned long part_size,
+                 const char *part_name, unsigned long long part_start, unsigned long long part_size,
                  unsigned short part_blocksize, int sys_ind)
 {
      struct partition_t*       part;
@@ -184,6 +187,41 @@ partition_fdisk_lookup( const char *dev_name, prom_handle disk,
      }
 }
 
+/* GPT partition format */
+static int gpt_magic_present(unsigned char *block_buffer, unsigned int prom_blksize)
+{
+       struct gpt_header *header = (struct gpt_header *)(block_buffer + prom_blksize);
+       return cpu_to_le64(header->signature) == GPT_HEADER_SIGNATURE;
+}
+
+static inline int
+guid_cmp (gpt_part_type_t left, gpt_part_type_t right)
+{
+       return memcmp(&left, &right, sizeof (gpt_part_type_t));
+}
+
+static void partition_gpt_lookup(prom_handle disk, unsigned int prom_blksize,
+                                                       struct partition_t **list)
+{
+       int partition;
+       struct gpt_header *header = (struct gpt_header *)(block_buffer + prom_blksize);
+
+       if (prom_readblocks(disk, cpu_to_le64(header->partitions), 1, block_buffer) != 1) {
+               prom_printf("Can't read GPT partition table %Lu\n", header->partitions);
+               return;
+       }
+       struct gpt_partition *part = (struct gpt_partition *)(block_buffer);
+
+       for (partition = 1; partition <= cpu_to_le32(header->maxpart); partition++, part++) {
+               if ((!guid_cmp(part->type, GPT_BASIC_DATA))||(!guid_cmp(part->type, GPT_LINUX_NATIVE))||(!guid_cmp(part->type, GPT_LINUX_RAID))) {
+                       add_new_partition(list, partition, "Linux", 0, le64_to_cpu(part->start), \
+                                               cpu_to_le64(part->end) - cpu_to_le64(part->start) + 1, \
+                                               prom_blksize, 1);
+               }
+       }
+}
+
+
 /* I don't know if it's possible to handle multisession and other multitrack
  * stuffs with the current OF disklabel package. This can still be implemented
  * with direct calls to atapi stuffs.
@@ -337,12 +375,12 @@ partitions_lookup(const char *device)
      struct partition_t* list = NULL;
      unsigned int prom_blksize, iso_root_block;
 
-     strncpy(block_buffer, device, 2040);
+     strncpy((char *)block_buffer, device, 2040);
      if (_machine != _MACH_bplan)
-         strcat(block_buffer, ":0");
+         strcat((char *)block_buffer, ":0");
 
      /* Open device */
-     disk = prom_open(block_buffer);
+     disk = prom_open((char *)block_buffer);
      if (disk == NULL) {
          prom_printf("Can't open device <%s>\n", block_buffer);
          goto bail;
@@ -357,14 +395,17 @@ partitions_lookup(const char *device)
          goto bail;
      }
 
-     /* Read boot blocs */
-     if (prom_readblocks(disk, 0, 1, block_buffer) != 1) {
+     /* Read boot blocks */
+     if (prom_readblocks(disk, 0, 2, block_buffer) != 1) {
          prom_printf("Can't read boot blocks\n");
          goto bail;
      }
      if (desc->signature == MAC_DRIVER_MAGIC) {
          /* pdisk partition format */
          partition_mac_lookup(device, disk, prom_blksize, &list);
+     } else if (gpt_magic_present(block_buffer, prom_blksize)) {
+         /* gpt partition format */
+         partition_gpt_lookup(disk, prom_blksize, &list);
      } else if ((block_buffer[510] == 0x55) && (block_buffer[511] == 0xaa)) {
          /* fdisk partition format */
          partition_fdisk_lookup(device, disk, prom_blksize, &list);
@@ -411,7 +452,7 @@ get_part_type(char *device, int partition)
          return '\0';
 
      for (p = parts; p && !found; p=p->next) {
-         DEBUG_F("number: %02d, start: 0x%08lx, length: 0x%08lx, type: %s, name: %s\n",
+         DEBUG_F("number: %02d, start: 0x%08Lx, length: 0x%08Lx, type: %s, name: %s\n",
                  p->part_number, p->part_start, p->part_size, p->part_type, p->part_name);
          if ((partition >= 0) && (partition == p->part_number)) {
               type = strdup(p->part_type);