summaryrefslogtreecommitdiffstats
path: root/grub-core/partmap/dfly.c
blob: d30da862477be43a79554db60874295bf96a22e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* dfly.c - Read DragonFly BSD disklabel64.  */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2013  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/disk.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/partition.h>

GRUB_MOD_LICENSE ("GPLv3+");

static struct grub_partition_map grub_dfly_partition_map;

#define GRUB_PARTITION_DISKLABEL64_HEADER_SIZE 200

/* Full entry is 64 bytes however we really care only
   about offset and size which are in first 16 bytes.
   Avoid using too much stack.  */
#define GRUB_PARTITION_DISKLABEL64_ENTRY_SIZE 64
struct grub_partition_disklabel64_entry
{
  grub_uint64_t boffset;
  grub_uint64_t bsize;
};

/* Full entry is 200 bytes however we really care only
   about magic and number of partitions which are in first 16 bytes.
   Avoid using too much stack.  */
struct grub_partition_disklabel64
{
  grub_uint32_t   magic;
#define GRUB_DISKLABEL64_MAGIC		  ((grub_uint32_t)0xc4464c59)
  grub_uint32_t   crc;
  grub_uint32_t   unused;
  grub_uint32_t   npartitions;
};

static grub_err_t
dfly_partition_map_iterate (grub_disk_t disk,
			    grub_partition_iterate_hook_t hook,
			    void *hook_data)
{
  struct grub_partition part;
  unsigned partno, pos;
  struct grub_partition_disklabel64 label;

  part.partmap = &grub_dfly_partition_map;

  if (grub_disk_read (disk, 1, 0, sizeof (label), &label))
    return grub_errno;

  if (label.magic != grub_cpu_to_le32_compile_time (GRUB_DISKLABEL64_MAGIC))
    {
      grub_dprintf ("partition",
		    "bad magic (found 0x%x; wanted 0x%x)\n",
		    (unsigned int) grub_le_to_cpu32 (label.magic),
		    (unsigned int) GRUB_DISKLABEL64_MAGIC);
      return grub_error (GRUB_ERR_BAD_PART_TABLE, "disklabel64 not found");
    }

  pos = GRUB_PARTITION_DISKLABEL64_HEADER_SIZE + GRUB_DISK_SECTOR_SIZE;

  for (partno = 0;
       partno < grub_le_to_cpu32 (label.npartitions); ++partno)
    {
      grub_disk_addr_t sector = pos >> GRUB_DISK_SECTOR_BITS;
      grub_off_t       offset = pos & (GRUB_DISK_SECTOR_SIZE - 1);
      struct grub_partition_disklabel64_entry dpart;

      pos += GRUB_PARTITION_DISKLABEL64_ENTRY_SIZE;
      if (grub_disk_read (disk, sector, offset, sizeof (dpart), &dpart))
	return grub_errno;

      grub_dprintf ("partition",
		    "partition %2d: offset 0x%llx, "
		    "size 0x%llx\n",
		    partno,
		    (unsigned long long) grub_le_to_cpu64 (dpart.boffset),
		    (unsigned long long) grub_le_to_cpu64 (dpart.bsize));

      /* Is partition initialized? */
      if (dpart.bsize == 0)
	continue;

      part.number = partno;
      part.start = grub_le_to_cpu64 (dpart.boffset) >> GRUB_DISK_SECTOR_BITS;
      part.len = grub_le_to_cpu64 (dpart.bsize) >> GRUB_DISK_SECTOR_BITS;

      /* This is counter-intuitive, but part.offset and sector have
       * the same type, and offset (NOT part.offset) is guaranteed
       * to fit into part.index. */
      part.offset = sector;
      part.index = offset;

      if (hook (disk, &part, hook_data))
	return grub_errno;
    }

  return GRUB_ERR_NONE;
}

/* Partition map type.  */
static struct grub_partition_map grub_dfly_partition_map =
{
    .name = "dfly",
    .iterate = dfly_partition_map_iterate,
};

GRUB_MOD_INIT(part_dfly)
{
  grub_partition_map_register (&grub_dfly_partition_map);
}

GRUB_MOD_FINI(part_dfly)
{
  grub_partition_map_unregister (&grub_dfly_partition_map);
}