Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / partmap / sun.c
blobdae36026995abf88d235440776758b231dbbd10a
1 /* sun.c - Read SUN style partition tables. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2005,2006,2007 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/partition.h>
21 #include <grub/disk.h>
22 #include <grub/mm.h>
23 #include <grub/misc.h>
24 #include <grub/dl.h>
25 #include <grub/symbol.h>
26 #include <grub/types.h>
27 #include <grub/err.h>
29 GRUB_MOD_LICENSE ("GPLv3+");
31 #define GRUB_PARTMAP_SUN_MAGIC 0xDABE
32 #define GRUB_PARTMAP_SUN_MAX_PARTS 8
33 #define GRUB_PARTMAP_SUN_WHOLE_DISK_ID 0x05
35 struct grub_sun_partition_info
37 grub_uint8_t spare1;
38 grub_uint8_t id;
39 grub_uint8_t spare2;
40 grub_uint8_t flags;
41 } GRUB_PACKED;
43 struct grub_sun_partition_descriptor
45 grub_uint32_t start_cylinder;
46 grub_uint32_t num_sectors;
47 } GRUB_PACKED;
49 struct grub_sun_block
51 grub_uint8_t info[128]; /* Informative text string. */
52 grub_uint8_t spare0[14];
53 struct grub_sun_partition_info infos[8];
54 grub_uint8_t spare1[246]; /* Boot information etc. */
55 grub_uint16_t rspeed; /* Disk rotational speed. */
56 grub_uint16_t pcylcount; /* Physical cylinder count. */
57 grub_uint16_t sparecyl; /* extra sects per cylinder. */
58 grub_uint8_t spare2[4]; /* More magic... */
59 grub_uint16_t ilfact; /* Interleave factor. */
60 grub_uint16_t ncyl; /* Data cylinder count. */
61 grub_uint16_t nacyl; /* Alt. cylinder count. */
62 grub_uint16_t ntrks; /* Tracks per cylinder. */
63 grub_uint16_t nsect; /* Sectors per track. */
64 grub_uint8_t spare3[4]; /* Even more magic... */
65 struct grub_sun_partition_descriptor partitions[8];
66 grub_uint16_t magic; /* Magic number. */
67 grub_uint16_t csum; /* Label xor'd checksum. */
68 } GRUB_PACKED;
70 static struct grub_partition_map grub_sun_partition_map;
72 /* Verify checksum (true=ok). */
73 static int
74 grub_sun_is_valid (grub_uint16_t *label)
76 grub_uint16_t *pos;
77 grub_uint16_t sum = 0;
79 for (pos = label;
80 pos < (label + sizeof (struct grub_sun_block) / 2);
81 pos++)
82 sum ^= *pos;
84 return ! sum;
87 static grub_err_t
88 sun_partition_map_iterate (grub_disk_t disk,
89 grub_partition_iterate_hook_t hook, void *hook_data)
91 struct grub_partition p;
92 union
94 struct grub_sun_block sun;
95 grub_uint16_t raw[0];
96 } block;
97 int partnum;
98 grub_err_t err;
100 p.partmap = &grub_sun_partition_map;
101 err = grub_disk_read (disk, 0, 0, sizeof (struct grub_sun_block),
102 &block);
103 if (err)
104 return err;
106 if (GRUB_PARTMAP_SUN_MAGIC != grub_be_to_cpu16 (block.sun.magic))
107 return grub_error (GRUB_ERR_BAD_PART_TABLE, "not a sun partition table");
109 if (! grub_sun_is_valid (block.raw))
110 return grub_error (GRUB_ERR_BAD_PART_TABLE, "invalid checksum");
112 /* Maybe another error value would be better, because partition
113 table _is_ recognized but invalid. */
114 for (partnum = 0; partnum < GRUB_PARTMAP_SUN_MAX_PARTS; partnum++)
116 struct grub_sun_partition_descriptor *desc;
118 if (block.sun.infos[partnum].id == 0
119 || block.sun.infos[partnum].id == GRUB_PARTMAP_SUN_WHOLE_DISK_ID)
120 continue;
122 desc = &block.sun.partitions[partnum];
123 p.start = ((grub_uint64_t) grub_be_to_cpu32 (desc->start_cylinder)
124 * grub_be_to_cpu16 (block.sun.ntrks)
125 * grub_be_to_cpu16 (block.sun.nsect));
126 p.len = grub_be_to_cpu32 (desc->num_sectors);
127 p.number = p.index = partnum;
128 if (p.len)
130 if (hook (disk, &p, hook_data))
131 partnum = GRUB_PARTMAP_SUN_MAX_PARTS;
135 return grub_errno;
138 /* Partition map type. */
139 static struct grub_partition_map grub_sun_partition_map =
141 .name = "sun",
142 .iterate = sun_partition_map_iterate,
145 GRUB_MOD_INIT(part_sun)
147 grub_partition_map_register (&grub_sun_partition_map);
150 GRUB_MOD_FINI(part_sun)
152 grub_partition_map_unregister (&grub_sun_partition_map);