1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/* ubootdisk.c - disk subsystem support for U-Boot platforms */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2013 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/disk.h>
#include <grub/err.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/partition.h>
#include <grub/term.h>
#include <grub/types.h>
#include <grub/uboot/disk.h>
#include <grub/uboot/uboot.h>
#include <grub/uboot/api_public.h>
static struct ubootdisk_data *hd_devices;
static int hd_num;
static int hd_max;
/*
* grub_ubootdisk_register():
* Called for each disk device enumerated as part of U-Boot initialization
* code.
*/
grub_err_t
grub_ubootdisk_register (struct device_info *newdev)
{
struct ubootdisk_data *d;
#define STOR_TYPE(x) ((x) & 0x0ff0)
switch (STOR_TYPE (newdev->type))
{
case DT_STOR_IDE:
case DT_STOR_SATA:
case DT_STOR_SCSI:
case DT_STOR_MMC:
case DT_STOR_USB:
/* hd */
if (hd_num == hd_max)
{
int new_num;
new_num = (hd_max ? hd_max * 2 : 1);
d = grub_realloc(hd_devices,
sizeof (struct ubootdisk_data) * new_num);
if (!d)
return grub_errno;
hd_devices = d;
hd_max = new_num;
}
d = &hd_devices[hd_num];
hd_num++;
break;
default:
return GRUB_ERR_BAD_DEVICE;
break;
}
d->dev = newdev;
d->cookie = newdev->cookie;
d->opencount = 0;
return 0;
}
/*
* uboot_disk_iterate():
* Iterator over enumerated disk devices.
*/
static int
uboot_disk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
char buf[16];
int count;
switch (pull)
{
case GRUB_DISK_PULL_NONE:
/* "hd" - built-in mass-storage */
for (count = 0 ; count < hd_num; count++)
{
grub_snprintf (buf, sizeof (buf) - 1, "hd%d", count);
grub_dprintf ("ubootdisk", "iterating %s\n", buf);
if (hook (buf, hook_data))
return 1;
}
break;
default:
return 0;
}
return 0;
}
/* Helper function for uboot_disk_open. */
static struct ubootdisk_data *
get_hd_device (int num)
{
if (num < hd_num)
return &hd_devices[num];
return NULL;
}
/*
* uboot_disk_open():
* Opens a disk device already enumerated.
*/
static grub_err_t
uboot_disk_open (const char *name, struct grub_disk *disk)
{
struct ubootdisk_data *d;
struct device_info *devinfo;
int num;
int retval;
grub_dprintf ("ubootdisk", "Opening '%s'\n", name);
num = grub_strtoul (name + 2, 0, 10);
if (grub_errno != GRUB_ERR_NONE)
{
grub_dprintf ("ubootdisk", "Opening '%s' failed, invalid number\n",
name);
goto fail;
}
if (name[1] != 'd')
{
grub_dprintf ("ubootdisk", "Opening '%s' failed, invalid name\n", name);
goto fail;
}
switch (name[0])
{
case 'h':
d = get_hd_device (num);
break;
default:
goto fail;
}
if (!d)
goto fail;
/*
* Subsystems may call open on the same device recursively - but U-Boot
* does not deal with this. So simply keep track of number of calls and
* return success if already open.
*/
if (d->opencount > 0)
{
grub_dprintf ("ubootdisk", "(%s) already open\n", disk->name);
d->opencount++;
retval = 0;
}
else
{
retval = grub_uboot_dev_open (d->dev);
if (retval != 0)
goto fail;
d->opencount = 1;
}
grub_dprintf ("ubootdisk", "cookie: 0x%08x\n", (grub_addr_t) d->cookie);
disk->id = (grub_addr_t) d->cookie;
devinfo = d->dev;
d->block_size = devinfo->di_stor.block_size;
if (d->block_size == 0)
return grub_error (GRUB_ERR_IO, "no block size");
for (disk->log_sector_size = 0;
(1U << disk->log_sector_size) < d->block_size;
disk->log_sector_size++);
grub_dprintf ("ubootdisk", "(%s) blocksize=%d, log_sector_size=%d\n",
disk->name, d->block_size, disk->log_sector_size);
if (devinfo->di_stor.block_count)
disk->total_sectors = devinfo->di_stor.block_count;
else
disk->total_sectors = GRUB_DISK_SIZE_UNKNOWN;
disk->data = d;
return GRUB_ERR_NONE;
fail:
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such device");
}
static void
uboot_disk_close (struct grub_disk *disk)
{
struct ubootdisk_data *d;
int retval;
d = disk->data;
/*
* In mirror of open function, keep track of number of calls to close and
* send on to U-Boot only when opencount would decrease to 0.
*/
if (d->opencount > 1)
{
grub_dprintf ("ubootdisk", "Closed (%s)\n", disk->name);
d->opencount--;
}
else if (d->opencount == 1)
{
retval = grub_uboot_dev_close (d->dev);
d->opencount--;
grub_dprintf ("ubootdisk", "closed %s (%d)\n", disk->name, retval);
}
else
{
grub_dprintf ("ubootdisk", "device %s not open!\n", disk->name);
}
}
/*
* uboot_disk_read():
* Called from within disk subsystem to read a sequence of blocks into the
* disk cache. Maps directly on top of U-Boot API, only wrap in some error
* handling.
*/
static grub_err_t
uboot_disk_read (struct grub_disk *disk,
grub_disk_addr_t offset, grub_size_t numblocks, char *buf)
{
struct ubootdisk_data *d;
grub_size_t real_size;
int retval;
d = disk->data;
retval = grub_uboot_dev_read (d->dev, buf, numblocks, offset, &real_size);
grub_dprintf ("ubootdisk",
"retval=%d, numblocks=%d, real_size=%llu, sector=%llu\n",
retval, numblocks, (grub_uint64_t) real_size,
(grub_uint64_t) offset);
if (retval != 0)
return grub_error (GRUB_ERR_IO, "U-Boot disk read error");
return GRUB_ERR_NONE;
}
static grub_err_t
uboot_disk_write (struct grub_disk *disk,
grub_disk_addr_t offset, grub_size_t numblocks, const char *buf)
{
struct ubootdisk_data *d;
int retval;
d = disk->data;
retval = grub_uboot_dev_write (d->dev, buf, numblocks, offset);
grub_dprintf ("ubootdisk",
"retval=%d, numblocks=%d, sector=%llu\n",
retval, numblocks, (grub_uint64_t) offset);
if (retval != 0)
return grub_error (GRUB_ERR_IO, "U-Boot disk write error");
return GRUB_ERR_NONE;
}
static struct grub_disk_dev grub_ubootdisk_dev = {
.name = "ubootdisk",
.id = GRUB_DISK_DEVICE_UBOOTDISK_ID,
.disk_iterate = uboot_disk_iterate,
.disk_open = uboot_disk_open,
.disk_close = uboot_disk_close,
.disk_read = uboot_disk_read,
.disk_write = uboot_disk_write,
.next = 0
};
void
grub_ubootdisk_init (void)
{
grub_disk_dev_register (&grub_ubootdisk_dev);
}
void
grub_ubootdisk_fini (void)
{
grub_disk_dev_unregister (&grub_ubootdisk_dev);
}
|