static grub_err_t grub_ofdisk_prepare (grub_disk_t disk, grub_disk_addr_t sector) { grub_ssize_t status; unsigned long long pos; if (disk->data != last_devpath) { if (last_ihandle) grub_ieee1275_close (last_ihandle); last_ihandle = 0; last_devpath = NULL; grub_ieee1275_open (disk->data, &last_ihandle); if (! last_ihandle) return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device"); last_devpath = disk->data; } pos = sector << disk->log_sector_size; grub_ieee1275_seek (last_ihandle, pos, &status); if (status < 0) return grub_error (GRUB_ERR_READ_ERROR, "seek error, can't seek block %llu", (long long) sector); return 0; }
static void card_close (const struct grub_net_card *dev) { struct grub_ofnetcard_data *data = dev->data; if (data->handle) grub_ieee1275_close (data->handle); }
void grub_ofdisk_fini (void) { if (last_ihandle) grub_ieee1275_close (last_ihandle); last_ihandle = 0; last_devpath = NULL; grub_disk_dev_unregister (&grub_ofdisk_dev); }
static grub_err_t grub_ofdisk_get_block_size (const char *device, grub_uint32_t *block_size, struct ofdisk_hash_ent *op) { struct size_args_ieee1275 { struct grub_ieee1275_common_hdr common; grub_ieee1275_cell_t method; grub_ieee1275_cell_t ihandle; grub_ieee1275_cell_t result; grub_ieee1275_cell_t size1; grub_ieee1275_cell_t size2; } args_ieee1275; if (last_ihandle) grub_ieee1275_close (last_ihandle); last_ihandle = 0; last_devpath = NULL; grub_ieee1275_open (device, &last_ihandle); if (! last_ihandle) return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device"); *block_size = 0; if (op->block_size_fails >= 2) return GRUB_ERR_NONE; INIT_IEEE1275_COMMON (&args_ieee1275.common, "call-method", 2, 2); args_ieee1275.method = (grub_ieee1275_cell_t) "block-size"; args_ieee1275.ihandle = last_ihandle; args_ieee1275.result = 1; if (IEEE1275_CALL_ENTRY_FN (&args_ieee1275) == -1) { grub_dprintf ("disk", "can't get block size: failed call-method\n"); op->block_size_fails++; } else if (args_ieee1275.result) { grub_dprintf ("disk", "can't get block size: %lld\n", (long long) args_ieee1275.result); op->block_size_fails++; } else if (args_ieee1275.size1 && !(args_ieee1275.size1 & (args_ieee1275.size1 - 1)) && args_ieee1275.size1 >= 512 && args_ieee1275.size1 <= 16384) { op->block_size_fails = 0; *block_size = args_ieee1275.size1; } return 0; }
static void grub_ofdisk_close (grub_disk_t disk) { if (disk->data == last_devpath) { if (last_ihandle) grub_ieee1275_close (last_ihandle); last_ihandle = 0; last_devpath = NULL; } disk->data = 0; }
static void grub_ofdisk_close (grub_disk_t disk) { struct ofdisk_hash_ent *data = disk->data; data->refs--; if (data->refs) return; grub_dprintf ("disk", "Closing handle %p.\n", data); grub_ieee1275_close (data->dev_ihandle); data->dev_ihandle = 0; }
grub_err_t grub_get_datetime (struct grub_datetime *datetime) { struct get_time_args { struct grub_ieee1275_common_hdr common; grub_ieee1275_cell_t method; grub_ieee1275_cell_t device; grub_ieee1275_cell_t catch_result; grub_ieee1275_cell_t year; grub_ieee1275_cell_t month; grub_ieee1275_cell_t day; grub_ieee1275_cell_t hour; grub_ieee1275_cell_t minute; grub_ieee1275_cell_t second; } args; int status; grub_ieee1275_ihandle_t ihandle; if (no_ieee1275_rtc) return grub_get_datetime_cmos (datetime); if (!rtc) find_rtc (); if (!rtc) return grub_get_datetime_cmos (datetime); status = grub_ieee1275_open (rtc, &ihandle); if (status == -1) return grub_error (GRUB_ERR_IO, "couldn't open RTC"); INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 7); args.device = (grub_ieee1275_cell_t) ihandle; args.method = (grub_ieee1275_cell_t) "get-time"; status = IEEE1275_CALL_ENTRY_FN (&args); grub_ieee1275_close (ihandle); if (status == -1 || args.catch_result) return grub_error (GRUB_ERR_IO, "get-time failed"); datetime->year = args.year; datetime->month = args.month; datetime->day = args.day; datetime->hour = args.hour; datetime->minute = args.minute; datetime->second = args.second; return GRUB_ERR_NONE; }
static grub_err_t grub_ofdisk_read (grub_disk_t disk, grub_disk_addr_t sector, grub_size_t size, char *buf) { grub_ssize_t status, actual; unsigned long long pos; if (disk->data != last_devpath) { if (last_ihandle) grub_ieee1275_close (last_ihandle); last_ihandle = 0; last_devpath = NULL; if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PARTITION_0)) { char name2[grub_strlen (disk->data) + 3]; char *p; grub_strcpy (name2, disk->data); p = name2 + grub_strlen (name2); *p++ = ':'; *p++ = '0'; *p = 0; grub_ieee1275_open (name2, &last_ihandle); } else grub_ieee1275_open (disk->data, &last_ihandle); if (! last_ihandle) return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device"); last_devpath = disk->data; } pos = sector * 512UL; grub_ieee1275_seek (last_ihandle, pos, &status); if (status < 0) return grub_error (GRUB_ERR_READ_ERROR, "seek error, can't seek block %llu", (long long) sector); grub_ieee1275_read (last_ihandle, buf, size * 512UL, &actual); if (actual != (grub_ssize_t) (size * 512UL)) return grub_error (GRUB_ERR_READ_ERROR, "read error on block: %llu", (long long) sector); return 0; }
static grub_err_t grub_ofdisk_prepare (grub_disk_t disk, grub_disk_addr_t sector) { grub_ssize_t status; unsigned long long pos; if (disk->data != last_devpath) { if (last_ihandle) grub_ieee1275_close (last_ihandle); last_ihandle = 0; last_devpath = NULL; if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PARTITION_0)) { char name2[grub_strlen (disk->data) + 3]; char *p; grub_strcpy (name2, disk->data); p = name2 + grub_strlen (name2); *p++ = ':'; *p++ = '0'; *p = 0; grub_ieee1275_open (name2, &last_ihandle); } else grub_ieee1275_open (disk->data, &last_ihandle); if (! last_ihandle) return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device"); last_devpath = disk->data; } pos = sector << GRUB_DISK_SECTOR_BITS; grub_ieee1275_seek (last_ihandle, pos, &status); if (status < 0) return grub_error (GRUB_ERR_READ_ERROR, "seek error, can't seek block %llu", (long long) sector); return 0; }
grub_err_t grub_ofdisk_get_block_size (const char *device, grub_uint32_t *block_size) { struct size_args_ieee1275 { struct grub_ieee1275_common_hdr common; grub_ieee1275_cell_t method; grub_ieee1275_cell_t ihandle; grub_ieee1275_cell_t result; grub_ieee1275_cell_t size1; grub_ieee1275_cell_t size2; } args_ieee1275; if (last_ihandle) grub_ieee1275_close (last_ihandle); last_ihandle = 0; last_devpath = NULL; grub_ieee1275_open (device, &last_ihandle); if (! last_ihandle) return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device"); INIT_IEEE1275_COMMON (&args_ieee1275.common, "call-method", 2, 2); args_ieee1275.method = (grub_ieee1275_cell_t) "block-size"; args_ieee1275.ihandle = last_ihandle; args_ieee1275.result = 1; *block_size = GRUB_DISK_SECTOR_SIZE; if ((IEEE1275_CALL_ENTRY_FN (&args_ieee1275) == -1) || (args_ieee1275.result)) grub_dprintf ("disk", "can't get block size\n"); else if (args_ieee1275.size1 && !(args_ieee1275.size1 & (args_ieee1275.size1 - 1)) && args_ieee1275.size1 >= 512 && args_ieee1275.size1 <= 16384) *block_size = args_ieee1275.size1; return 0; }
static void scan (void) { auto int dev_iterate_real (const char *name, const char *path); int dev_iterate_real (const char *name, const char *path) { struct ofdisk_hash_ent *op; grub_dprintf ("disk", "disk name = %s, path = %s\n", name, path); op = ofdisk_hash_find (path); if (!op) { char *name_dup = grub_strdup (name); char *can = grub_strdup (path); if (!name_dup || !can) { grub_errno = GRUB_ERR_NONE; grub_free (name_dup); grub_free (can); return 0; } op = ofdisk_hash_add (name_dup, can); } return 0; } auto int dev_iterate_alias (struct grub_ieee1275_devalias *alias); int dev_iterate_alias (struct grub_ieee1275_devalias *alias) { if (grub_strcmp (alias->type, "block") != 0) return 0; return dev_iterate_real (alias->name, alias->path); } auto int dev_iterate (struct grub_ieee1275_devalias *alias); int dev_iterate (struct grub_ieee1275_devalias *alias) { if (grub_strcmp (alias->type, "vscsi") == 0) { static grub_ieee1275_ihandle_t ihandle; struct set_color_args { struct grub_ieee1275_common_hdr common; grub_ieee1275_cell_t method; grub_ieee1275_cell_t ihandle; grub_ieee1275_cell_t catch_result; grub_ieee1275_cell_t nentries; grub_ieee1275_cell_t table; } args; char *buf, *bufptr; unsigned i; if (grub_ieee1275_open (alias->path, &ihandle)) return 0; INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 3); args.method = (grub_ieee1275_cell_t) "vscsi-report-luns"; args.ihandle = ihandle; args.table = 0; args.nentries = 0; if (IEEE1275_CALL_ENTRY_FN (&args) == -1) { grub_ieee1275_close (ihandle); return 0; } buf = grub_malloc (grub_strlen (alias->path) + 32); if (!buf) return 0; bufptr = grub_stpcpy (buf, alias->path); for (i = 0; i < args.nentries; i++) { grub_uint64_t *ptr; ptr = *(grub_uint64_t **) (args.table + 4 + 8 * i); while (*ptr) { grub_snprintf (bufptr, 32, "/disk@%" PRIxGRUB_UINT64_T, *ptr++); if (dev_iterate_real (buf, buf)) return 1; } } grub_ieee1275_close (ihandle); grub_free (buf); return 0; } if (!grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_TREE_SCANNING_FOR_DISKS) && grub_strcmp (alias->type, "block") == 0) return dev_iterate_real (alias->path, alias->path); return grub_children_iterate (alias->path, dev_iterate); } grub_devalias_iterate (dev_iterate_alias); grub_children_iterate ("/", dev_iterate); }
static grub_err_t grub_ofdisk_open (const char *name, grub_disk_t disk) { grub_ieee1275_phandle_t dev; grub_ieee1275_ihandle_t dev_ihandle = 0; struct ofdisk_hash_ent *op; char *devpath; /* XXX: This should be large enough for any possible case. */ char prop[64]; grub_ssize_t actual; devpath = compute_dev_path (name); if (! devpath) return grub_errno; op = ofdisk_hash_find (devpath); if (!op) op = ofdisk_hash_add (devpath); grub_free (devpath); if (!op) return grub_errno; if (op->dev_ihandle) { op->refs++; /* XXX: There is no property to read the number of blocks. There should be a property `#blocks', but it is not there. Perhaps it is possible to use seek for this. */ disk->total_sectors = 0xFFFFFFFFUL; disk->id = (unsigned long) op; /* XXX: Read this, somehow. */ disk->has_partitions = 1; disk->data = op; return 0; } grub_dprintf ("disk", "Opening `%s'.\n", op->devpath); if (grub_ieee1275_finddevice (op->devpath, &dev)) { grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't read device properties"); goto fail; } if (grub_ieee1275_get_property (dev, "device_type", prop, sizeof (prop), &actual)) { grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't read the device type"); goto fail; } if (grub_strcmp (prop, "block")) { grub_error (GRUB_ERR_BAD_DEVICE, "not a block device"); goto fail; } grub_ieee1275_open (op->devpath, &dev_ihandle); if (! dev_ihandle) { grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device"); goto fail; } grub_dprintf ("disk", "Opened `%s' as handle %p.\n", op->devpath, (void *) (unsigned long) dev_ihandle); op->dev_ihandle = dev_ihandle; op->refs++; /* XXX: There is no property to read the number of blocks. There should be a property `#blocks', but it is not there. Perhaps it is possible to use seek for this. */ disk->total_sectors = 0xFFFFFFFFUL; disk->id = (unsigned long) op; /* XXX: Read this, somehow. */ disk->has_partitions = 1; disk->data = op; return 0; fail: if (dev_ihandle) grub_ieee1275_close (dev_ihandle); return grub_errno; }
static void dev_iterate (const struct grub_ieee1275_devalias *alias) { if (grub_strcmp (alias->type, "vscsi") == 0) { static grub_ieee1275_ihandle_t ihandle; struct set_color_args { struct grub_ieee1275_common_hdr common; grub_ieee1275_cell_t method; grub_ieee1275_cell_t ihandle; grub_ieee1275_cell_t catch_result; grub_ieee1275_cell_t nentries; grub_ieee1275_cell_t table; } args; char *buf, *bufptr; unsigned i; if (grub_ieee1275_open (alias->path, &ihandle)) return; /* This method doesn't need memory allocation for the table. Open firmware takes care of all memory management and the result table stays in memory and is never freed. */ INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 3); args.method = (grub_ieee1275_cell_t) "vscsi-report-luns"; args.ihandle = ihandle; args.table = 0; args.nentries = 0; if (IEEE1275_CALL_ENTRY_FN (&args) == -1 || args.catch_result) { grub_ieee1275_close (ihandle); return; } buf = grub_malloc (grub_strlen (alias->path) + 32); if (!buf) return; bufptr = grub_stpcpy (buf, alias->path); for (i = 0; i < args.nentries; i++) { grub_uint64_t *ptr; ptr = *(grub_uint64_t **) (args.table + 4 + 8 * i); while (*ptr) { grub_snprintf (bufptr, 32, "/disk@%" PRIxGRUB_UINT64_T, *ptr++); dev_iterate_real (buf, buf); } } grub_ieee1275_close (ihandle); grub_free (buf); return; } else if (grub_strcmp (alias->type, "sas_ioa") == 0) { /* The method returns the number of disks and a table where * each ID is 64-bit long. Example of sas paths: * /pci@80000002000001f/pci1014,034A@0/sas/disk@c05db70800 * /pci@80000002000001f/pci1014,034A@0/sas/disk@a05db70800 * /pci@80000002000001f/pci1014,034A@0/sas/disk@805db70800 */ struct sas_children { struct grub_ieee1275_common_hdr common; grub_ieee1275_cell_t method; grub_ieee1275_cell_t ihandle; grub_ieee1275_cell_t max; grub_ieee1275_cell_t table; grub_ieee1275_cell_t catch_result; grub_ieee1275_cell_t nentries; } args; char *buf, *bufptr; unsigned i; grub_uint64_t *table; grub_uint16_t table_size; grub_ieee1275_ihandle_t ihandle; buf = grub_malloc (grub_strlen (alias->path) + sizeof ("/disk@7766554433221100")); if (!buf) return; bufptr = grub_stpcpy (buf, alias->path); /* Power machines documentation specify 672 as maximum SAS disks in one system. Using a slightly larger value to be safe. */ table_size = 768; table = grub_malloc (table_size * sizeof (grub_uint64_t)); if (!table) { grub_free (buf); return; } if (grub_ieee1275_open (alias->path, &ihandle)) { grub_free (buf); grub_free (table); return; } INIT_IEEE1275_COMMON (&args.common, "call-method", 4, 2); args.method = (grub_ieee1275_cell_t) "get-sas-children"; args.ihandle = ihandle; args.max = table_size; args.table = (grub_ieee1275_cell_t) table; args.catch_result = 0; args.nentries = 0; if (IEEE1275_CALL_ENTRY_FN (&args) == -1) { grub_ieee1275_close (ihandle); grub_free (table); grub_free (buf); return; } for (i = 0; i < args.nentries; i++) { grub_snprintf (bufptr, sizeof ("/disk@7766554433221100"), "/disk@%" PRIxGRUB_UINT64_T, table[i]); dev_iterate_real (buf, buf); } grub_ieee1275_close (ihandle); grub_free (table); grub_free (buf); } if (!grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_TREE_SCANNING_FOR_DISKS) && grub_strcmp (alias->type, "block") == 0) { dev_iterate_real (alias->path, alias->path); return; } { struct grub_ieee1275_devalias child; FOR_IEEE1275_DEVCHILDREN(alias->path, child) dev_iterate (&child); } }
static void dev_iterate (const struct grub_ieee1275_devalias *alias) { if (grub_strcmp (alias->type, "vscsi") == 0) { static grub_ieee1275_ihandle_t ihandle; struct set_color_args { struct grub_ieee1275_common_hdr common; grub_ieee1275_cell_t method; grub_ieee1275_cell_t ihandle; grub_ieee1275_cell_t catch_result; grub_ieee1275_cell_t nentries; grub_ieee1275_cell_t table; } args; char *buf, *bufptr; unsigned i; if (grub_ieee1275_open (alias->path, &ihandle)) return; INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 3); args.method = (grub_ieee1275_cell_t) "vscsi-report-luns"; args.ihandle = ihandle; args.table = 0; args.nentries = 0; if (IEEE1275_CALL_ENTRY_FN (&args) == -1 || args.catch_result) { grub_ieee1275_close (ihandle); return; } buf = grub_malloc (grub_strlen (alias->path) + 32); if (!buf) return; bufptr = grub_stpcpy (buf, alias->path); for (i = 0; i < args.nentries; i++) { grub_uint64_t *ptr; ptr = *(grub_uint64_t **) (args.table + 4 + 8 * i); while (*ptr) { grub_snprintf (bufptr, 32, "/disk@%" PRIxGRUB_UINT64_T, *ptr++); dev_iterate_real (buf, buf); } } grub_ieee1275_close (ihandle); grub_free (buf); return; } if (!grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_TREE_SCANNING_FOR_DISKS) && grub_strcmp (alias->type, "block") == 0) { dev_iterate_real (alias->path, alias->path); return; } { struct grub_ieee1275_devalias child; FOR_IEEE1275_DEVCHILDREN(alias->path, child) dev_iterate (&child); } }