]> git.proxmox.com Git - grub2.git/blame - partmap/amiga.c
2005-11-13 Marco Gerards <mgerards@xs4all.nl>
[grub2.git] / partmap / amiga.c
CommitLineData
3f1578fe 1/* amiga.c - Read amiga partition tables (RDB). */
2/*
3 * GRUB -- GRand Unified Bootloader
6d099807 4 * Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
3f1578fe 5 *
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <grub/disk.h>
22#include <grub/misc.h>
23#include <grub/mm.h>
24#include <grub/partition.h>
25#include <grub/dl.h>
26
27struct grub_amiga_rdsk
28{
29 /* "RDSK". */
30 grub_uint8_t magic[4];
31 grub_uint32_t size;
32 grub_int32_t checksum;
33 grub_uint32_t scsihost;
34 grub_uint32_t blksz;
35 grub_uint32_t flags;
36 grub_uint32_t badblcklst;
37 grub_uint32_t partitionlst;
38 grub_uint32_t fslst;
39
40 /* The other information is not important for us. */
41} __attribute__ ((packed));
42
43struct grub_amiga_partition
44{
45 /* "PART". */
46 grub_uint8_t magic[4];
47 grub_int32_t size;
48 grub_int32_t checksum;
49 grub_uint32_t scsihost;
50 grub_uint32_t next;
51 grub_uint32_t flags;
52 grub_uint32_t unused1[2];
53 grub_uint32_t devflags;
54 grub_uint8_t namelen;
55 grub_uint8_t name[31];
56 grub_uint32_t unused2[15];
57
58 grub_uint32_t unused3[3];
59 grub_uint32_t heads;
60 grub_uint32_t unused4;
61 grub_uint32_t block_per_track;
62 grub_uint32_t unused5[3];
63 grub_uint32_t lowcyl;
64 grub_uint32_t highcyl;
65
66 grub_uint32_t firstcyl;
67} __attribute__ ((packed));
68
69static struct grub_partition_map grub_amiga_partition_map;
70
71#ifndef GRUB_UTIL
72static grub_dl_t my_mod;
73#endif
992ffbbe 74
3f1578fe 75\f
992ffbbe 76
3f1578fe 77static grub_err_t
78amiga_partition_map_iterate (grub_disk_t disk,
992ffbbe 79 int (*hook) (grub_disk_t disk,
80 const grub_partition_t partition))
3f1578fe 81{
82 struct grub_partition part;
83 struct grub_amiga_rdsk rdsk;
84 struct grub_disk raw;
85 int partno = 0;
86 int next = -1;
87 int pos;
88
89 /* Enforce raw disk access. */
90 raw = *disk;
91 raw.partition = 0;
92
93 /* The RDSK block is one of the first 15 blocks. */
94 for (pos = 0; pos < 15; pos++)
95 {
96 /* Read the RDSK block which is a descriptor for the entire disk. */
97 if (grub_disk_read (&raw, pos, 0,
98 sizeof (rdsk), (char *) &rdsk))
99 return grub_errno;
100
101 if (!grub_strcmp (rdsk.magic, "RDSK"))
102 {
103 /* Found the first PART block. */
104 next = grub_be_to_cpu32 (rdsk.partitionlst);
105 break;
106 }
107 }
1b14a681 108
109 if (next == -1)
110 return grub_error (GRUB_ERR_BAD_PART_TABLE,
111 "Amiga partition map not found.");
3f1578fe 112
113 /* The end of the partition list is marked using "-1". */
114 while (next != -1)
115 {
116 struct grub_amiga_partition apart;
117
118 /* Read the RDSK block which is a descriptor for the entire disk. */
119 if (grub_disk_read (&raw, next, 0,
120 sizeof (apart), (char *) &apart))
121 return grub_errno;
122
123 /* Calculate the first block and the size of the partition. */
124 part.start = (grub_be_to_cpu32 (apart.lowcyl)
125 * grub_be_to_cpu32 (apart.heads)
126 * grub_be_to_cpu32 (apart.block_per_track));
127 part.len = ((grub_be_to_cpu32 (apart.highcyl)
128 - grub_be_to_cpu32 (apart.lowcyl) + 1)
129 * grub_be_to_cpu32 (apart.heads)
130 * grub_be_to_cpu32 (apart.block_per_track));
131
132 part.offset = next * 512;
133 part.index = partno;
134 part.partmap = &grub_amiga_partition_map;
135
992ffbbe 136 if (hook (disk, &part))
3f1578fe 137 return grub_errno;
138
139 next = grub_be_to_cpu32 (apart.next);
140 partno++;
141 }
142
143 return 0;
144}
145
146
147static grub_partition_t
148amiga_partition_map_probe (grub_disk_t disk, const char *str)
149{
150 grub_partition_t p = 0;
151 int partnum = 0;
152 char *s = (char *) str;
153
992ffbbe 154 auto int find_func (grub_disk_t d, const grub_partition_t partition);
3f1578fe 155
992ffbbe 156 int find_func (grub_disk_t d __attribute__ ((unused)),
157 const grub_partition_t partition)
3f1578fe 158 {
159 if (partnum == partition->index)
160 {
161 p = (grub_partition_t) grub_malloc (sizeof (*p));
162 if (! p)
163 return 1;
164
165 grub_memcpy (p, partition, sizeof (*p));
166 return 1;
167 }
168
169 return 0;
170 }
171
172 /* Get the partition number. */
173 partnum = grub_strtoul (s, 0, 10);
174 if (grub_errno)
175 {
176 grub_error (GRUB_ERR_BAD_FILENAME, "invalid partition");
177 return 0;
178 }
179
180 if (amiga_partition_map_iterate (disk, find_func))
181 goto fail;
182
183 return p;
184
185 fail:
186 grub_free (p);
187 return 0;
188}
189
190
191static char *
192amiga_partition_map_get_name (const grub_partition_t p)
193{
194 char *name;
195
196 name = grub_malloc (13);
197 if (! name)
198 return 0;
199
200 grub_sprintf (name, "%d", p->index);
201 return name;
202}
203
204\f
205/* Partition map type. */
206static struct grub_partition_map grub_amiga_partition_map =
207 {
208 .name = "amiga_partition_map",
209 .iterate = amiga_partition_map_iterate,
210 .probe = amiga_partition_map_probe,
211 .get_name = amiga_partition_map_get_name
212 };
213
6d099807 214GRUB_MOD_INIT(amiga_partition_map)
3f1578fe 215{
216 grub_partition_map_register (&grub_amiga_partition_map);
6d099807 217#ifndef GRUB_UTIL
3f1578fe 218 my_mod = mod;
6d099807 219#endif
3f1578fe 220}
221
6d099807 222GRUB_MOD_FINI(amiga_partition_map)
3f1578fe 223{
224 grub_partition_map_unregister (&grub_amiga_partition_map);
225}