1
/* ofdisk.c - Open Firmware disk access. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2004 Free Software Foundation, Inc.
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.
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.
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.
21
#include <grub/misc.h>
22
#include <grub/disk.h>
24
#include <grub/ieee1275/ieee1275.h>
25
#include <grub/ieee1275/ofdisk.h>
28
grub_ofdisk_iterate (int (*hook) (const char *name))
30
auto int dev_iterate (struct grub_ieee1275_devalias *alias);
32
int dev_iterate (struct grub_ieee1275_devalias *alias)
34
if (! grub_strcmp (alias->type, "block"))
36
else if ((! grub_strcmp (alias->type, "scsi"))
37
|| (! grub_strcmp (alias->type, "ide"))
38
|| (! grub_strcmp (alias->type, "ata")))
39
/* Search for block-type children of these bus controllers. */
40
grub_children_iterate (alias->name, dev_iterate);
44
grub_devalias_iterate (dev_iterate);
49
grub_ofdisk_open (const char *name, grub_disk_t disk)
51
grub_ieee1275_phandle_t dev;
52
grub_ieee1275_ihandle_t dev_ihandle = 0;
54
/* XXX: This should be large enough for any possible case. */
58
devpath = grub_strndup (name, grub_strlen (name) + 2);
62
/* To access the complete disk add `:0'. */
63
if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PARTITION_0))
64
grub_strcat (devpath, ":0");
66
grub_dprintf ("disk", "Opening `%s'.\n", devpath);
68
grub_ieee1275_open (devpath, &dev_ihandle);
71
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Can't open device");
75
grub_dprintf ("disk", "Opened `%s' as handle %p.\n", devpath, (void *) dev_ihandle);
77
if (grub_ieee1275_finddevice (devpath, &dev))
79
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Can't read device properties");
83
if (grub_ieee1275_get_property (dev, "device_type", prop, sizeof (prop),
86
grub_error (GRUB_ERR_BAD_DEVICE, "Can't read the device type");
90
if (grub_strcmp (prop, "block"))
92
grub_error (GRUB_ERR_BAD_DEVICE, "Not a block device");
96
/* XXX: There is no property to read the number of blocks. There
97
should be a property `#blocks', but it is not there. Perhaps it
98
is possible to use seek for this. */
99
disk->total_sectors = 0xFFFFFFFFUL;
101
/* XXX: Is it ok to use this? Perhaps it is better to use the path
105
/* XXX: Read this, somehow. */
106
disk->has_partitions = 1;
107
disk->data = (void *) dev_ihandle;
110
if (grub_errno && dev_ihandle)
111
grub_ieee1275_close (dev_ihandle);
117
grub_ofdisk_close (grub_disk_t disk)
119
grub_dprintf ("disk", "Closing handle %p.\n",
120
(void *) disk->data);
121
grub_ieee1275_close ((grub_ieee1275_ihandle_t) disk->data);
125
grub_ofdisk_read (grub_disk_t disk, unsigned long sector,
126
unsigned long size, char *buf)
128
grub_ssize_t status, actual;
129
unsigned long long pos;
131
grub_dprintf ("disk",
132
"Reading handle %p: sector 0x%lx, size 0x%lx, buf %p.\n",
133
(void *) disk->data, sector, size, buf);
135
pos = (unsigned long long) sector * 512UL;
137
grub_ieee1275_seek ((grub_ieee1275_ihandle_t) disk->data, (int) (pos >> 32),
138
(int) pos & 0xFFFFFFFFUL, &status);
140
return grub_error (GRUB_ERR_READ_ERROR,
141
"Seek error, can't seek block %d", sector);
142
grub_ieee1275_read ((grub_ieee1275_ihandle_t) disk->data, buf,
143
size * 512UL, &actual);
144
if (actual != actual)
145
return grub_error (GRUB_ERR_READ_ERROR, "Read error on block: %d", sector);
151
grub_ofdisk_write (grub_disk_t disk __attribute ((unused)),
152
unsigned long sector __attribute ((unused)),
153
unsigned long size __attribute ((unused)),
154
const char *buf __attribute ((unused)))
156
return GRUB_ERR_NOT_IMPLEMENTED_YET;
159
static struct grub_disk_dev grub_ofdisk_dev =
162
.id = GRUB_DISK_DEVICE_OFDISK_ID,
163
.iterate = grub_ofdisk_iterate,
164
.open = grub_ofdisk_open,
165
.close = grub_ofdisk_close,
166
.read = grub_ofdisk_read,
167
.write = grub_ofdisk_write,
172
grub_ofdisk_init (void)
174
grub_disk_dev_register (&grub_ofdisk_dev);
178
grub_ofdisk_fini (void)
180
grub_disk_dev_unregister (&grub_ofdisk_dev);