1
/* ofdisk.c - Open Firmware disk access. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2004,2006,2007,2008,2009 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/misc.h>
21
#include <grub/disk.h>
23
#include <grub/ieee1275/ieee1275.h>
24
#include <grub/ieee1275/ofdisk.h>
26
static char *last_devpath;
27
static grub_ieee1275_ihandle_t last_ihandle;
29
struct ofdisk_hash_ent
32
/* Pointer to shortest available name on nodes representing canonical names,
35
struct ofdisk_hash_ent *next;
38
#define OFDISK_HASH_SZ 8
39
static struct ofdisk_hash_ent *ofdisk_hash[OFDISK_HASH_SZ];
42
ofdisk_hash_fn (const char *devpath)
47
return (hash & (OFDISK_HASH_SZ - 1));
50
static struct ofdisk_hash_ent *
51
ofdisk_hash_find (const char *devpath)
53
struct ofdisk_hash_ent *p = ofdisk_hash[ofdisk_hash_fn(devpath)];
57
if (!grub_strcmp (p->devpath, devpath))
64
static struct ofdisk_hash_ent *
65
ofdisk_hash_add (char *devpath)
67
struct ofdisk_hash_ent **head = &ofdisk_hash[ofdisk_hash_fn(devpath)];
68
struct ofdisk_hash_ent *p, *pcan;
71
p = grub_malloc(sizeof (*p));
80
curcan = grub_ieee1275_canonicalise_devname (devpath);
83
grub_errno = GRUB_ERR_NONE;
87
pcan = ofdisk_hash_find (curcan);
89
pcan = ofdisk_hash_add (curcan);
94
grub_errno = GRUB_ERR_NONE;
98
|| grub_strlen (pcan->shortest) > grub_strlen (devpath))
99
pcan->shortest = devpath;
108
auto int dev_iterate (struct grub_ieee1275_devalias *alias);
110
int dev_iterate (struct grub_ieee1275_devalias *alias)
112
struct ofdisk_hash_ent *op;
114
grub_dprintf ("disk", "device name = %s type = %s\n", alias->name,
117
if (grub_strcmp (alias->type, "block") != 0)
120
grub_dprintf ("disk", "disk name = %s\n", alias->name);
122
op = ofdisk_hash_find (alias->path);
125
char *name = grub_strdup (alias->name);
128
grub_errno = GRUB_ERR_NONE;
131
op = ofdisk_hash_add (name);
136
grub_devalias_iterate (dev_iterate);
137
grub_ieee1275_devices_iterate (dev_iterate);
141
grub_ofdisk_iterate (int (*hook) (const char *name))
146
for (i = 0; i < ARRAY_SIZE (ofdisk_hash); i++)
148
static struct ofdisk_hash_ent *ent;
149
for (ent = ofdisk_hash[i]; ent; ent = ent->next)
153
if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_OFDISK_SDCARD_ONLY))
155
grub_ieee1275_phandle_t dev;
158
if (grub_ieee1275_finddevice (ent->devpath, &dev))
160
grub_dprintf ("disk", "finddevice (%s) failed\n",
165
if (grub_ieee1275_get_property (dev, "iconname", tmp,
168
grub_dprintf ("disk", "get iconname failed\n");
172
if (grub_strcmp (tmp, "sdmmc") != 0)
174
grub_dprintf ("disk", "device is not an SD card\n");
179
if (grub_strncmp (ent->shortest, "cdrom", 5) == 0)
182
if (hook (ent->shortest))
190
compute_dev_path (const char *name)
192
char *devpath = grub_malloc (grub_strlen (name) + 3);
198
/* Un-escape commas. */
200
while ((c = *name++) != '\0')
202
if (c == '\\' && *name == ',')
217
grub_ofdisk_open (const char *name, grub_disk_t disk)
219
grub_ieee1275_phandle_t dev;
221
/* XXX: This should be large enough for any possible case. */
225
devpath = compute_dev_path (name);
229
grub_dprintf ("disk", "Opening `%s'.\n", devpath);
231
if (grub_ieee1275_finddevice (devpath, &dev))
232
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't read device properties");
234
if (grub_ieee1275_get_property (dev, "device_type", prop, sizeof (prop),
236
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't read the device type");
238
if (grub_strcmp (prop, "block"))
239
return grub_error (GRUB_ERR_BAD_DEVICE, "not a block device");
241
/* XXX: There is no property to read the number of blocks. There
242
should be a property `#blocks', but it is not there. Perhaps it
243
is possible to use seek for this. */
244
disk->total_sectors = GRUB_DISK_SIZE_UNKNOWN;
247
struct ofdisk_hash_ent *op;
248
op = ofdisk_hash_find (devpath);
250
op = ofdisk_hash_add (devpath);
255
disk->id = (unsigned long) op;
256
disk->data = op->devpath;
263
grub_ofdisk_close (grub_disk_t disk)
265
if (disk->data == last_devpath)
268
grub_ieee1275_close (last_ihandle);
276
grub_ofdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
277
grub_size_t size, char *buf)
279
grub_ssize_t status, actual;
280
unsigned long long pos;
282
if (disk->data != last_devpath)
285
grub_ieee1275_close (last_ihandle);
289
if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PARTITION_0))
291
char name2[grub_strlen (disk->data) + 3];
294
grub_strcpy (name2, disk->data);
295
p = name2 + grub_strlen (name2);
299
grub_ieee1275_open (name2, &last_ihandle);
302
grub_ieee1275_open (disk->data, &last_ihandle);
304
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
305
last_devpath = disk->data;
308
pos = sector * 512UL;
310
grub_ieee1275_seek (last_ihandle, pos, &status);
312
return grub_error (GRUB_ERR_READ_ERROR,
313
"seek error, can't seek block %llu",
315
grub_ieee1275_read (last_ihandle, buf, size * 512UL, &actual);
316
if (actual != (grub_ssize_t) (size * 512UL))
317
return grub_error (GRUB_ERR_READ_ERROR, "read error on block: %llu",
324
grub_ofdisk_write (grub_disk_t disk __attribute ((unused)),
325
grub_disk_addr_t sector __attribute ((unused)),
326
grub_size_t size __attribute ((unused)),
327
const char *buf __attribute ((unused)))
329
return GRUB_ERR_NOT_IMPLEMENTED_YET;
332
static struct grub_disk_dev grub_ofdisk_dev =
335
.id = GRUB_DISK_DEVICE_OFDISK_ID,
336
.iterate = grub_ofdisk_iterate,
337
.open = grub_ofdisk_open,
338
.close = grub_ofdisk_close,
339
.read = grub_ofdisk_read,
340
.write = grub_ofdisk_write,
345
grub_ofdisk_init (void)
347
grub_disk_dev_register (&grub_ofdisk_dev);
351
grub_ofdisk_fini (void)
354
grub_ieee1275_close (last_ihandle);
358
grub_disk_dev_unregister (&grub_ofdisk_dev);