243 lines
6.5 KiB
C
243 lines
6.5 KiB
C
/* nand.c - NAND flash disk access. */
|
|
/*
|
|
* VAS_EBOOT -- GRand Unified Bootloader
|
|
* Copyright (C) 2008,2009 Free Software Foundation, Inc.
|
|
*
|
|
* VAS_EBOOT 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.
|
|
*
|
|
* VAS_EBOOT 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 VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <VasEBoot/misc.h>
|
|
#include <VasEBoot/disk.h>
|
|
#include <VasEBoot/mm.h>
|
|
#include <VasEBoot/dl.h>
|
|
#include <VasEBoot/ieee1275/ieee1275.h>
|
|
#include <VasEBoot/i18n.h>
|
|
|
|
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
|
|
|
|
struct VasEBoot_nand_data
|
|
{
|
|
VasEBoot_ieee1275_ihandle_t handle;
|
|
VasEBoot_uint32_t block_size;
|
|
};
|
|
|
|
static int
|
|
VasEBoot_nand_iterate (VasEBoot_disk_dev_iterate_hook_t hook, void *hook_data,
|
|
VasEBoot_disk_pull_t pull)
|
|
{
|
|
static int have_nand = -1;
|
|
|
|
if (pull != VAS_EBOOT_DISK_PULL_NONE)
|
|
return 0;
|
|
|
|
if (have_nand == -1)
|
|
{
|
|
struct VasEBoot_ieee1275_devalias alias;
|
|
|
|
have_nand = 0;
|
|
FOR_IEEE1275_DEVALIASES(alias)
|
|
if (VasEBoot_strcmp (alias.name, "nand") == 0)
|
|
{
|
|
have_nand = 1;
|
|
break;
|
|
}
|
|
VasEBoot_ieee1275_devalias_free (&alias);
|
|
}
|
|
|
|
if (have_nand)
|
|
return hook ("nand", hook_data);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static VasEBoot_err_t
|
|
VasEBoot_nand_read (VasEBoot_disk_t disk, VasEBoot_disk_addr_t sector,
|
|
VasEBoot_size_t size, char *buf);
|
|
|
|
static VasEBoot_err_t
|
|
VasEBoot_nand_open (const char *name, VasEBoot_disk_t disk)
|
|
{
|
|
VasEBoot_ieee1275_ihandle_t dev_ihandle = 0;
|
|
struct VasEBoot_nand_data *data = 0;
|
|
const char *devname;
|
|
struct size_args
|
|
{
|
|
struct VasEBoot_ieee1275_common_hdr common;
|
|
VasEBoot_ieee1275_cell_t method;
|
|
VasEBoot_ieee1275_cell_t ihandle;
|
|
VasEBoot_ieee1275_cell_t result;
|
|
VasEBoot_ieee1275_cell_t size1;
|
|
VasEBoot_ieee1275_cell_t size2;
|
|
} args;
|
|
|
|
if (VasEBoot_memcmp (name, "nand/", sizeof ("nand/") - 1) == 0)
|
|
devname = name + sizeof ("nand/") - 1;
|
|
else if (VasEBoot_strcmp (name, "nand") == 0)
|
|
devname = name;
|
|
else
|
|
return VasEBoot_error (VAS_EBOOT_ERR_UNKNOWN_DEVICE, "not a NAND device");
|
|
|
|
data = VasEBoot_malloc (sizeof (*data));
|
|
if (! data)
|
|
goto fail;
|
|
|
|
VasEBoot_ieee1275_open (devname, &dev_ihandle);
|
|
if (! dev_ihandle)
|
|
{
|
|
VasEBoot_error (VAS_EBOOT_ERR_UNKNOWN_DEVICE, "can't open device");
|
|
goto fail;
|
|
}
|
|
|
|
data->handle = dev_ihandle;
|
|
|
|
INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 2);
|
|
args.method = (VasEBoot_ieee1275_cell_t) "block-size";
|
|
args.ihandle = dev_ihandle;
|
|
args.result = 1;
|
|
|
|
if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.result))
|
|
{
|
|
VasEBoot_error (VAS_EBOOT_ERR_UNKNOWN_DEVICE, "can't get block size");
|
|
goto fail;
|
|
}
|
|
|
|
data->block_size = (args.size1 >> VAS_EBOOT_DISK_SECTOR_BITS);
|
|
if (!data->block_size)
|
|
{
|
|
VasEBoot_error (VAS_EBOOT_ERR_UNKNOWN_DEVICE, "invalid block size");
|
|
goto fail;
|
|
}
|
|
|
|
INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 3);
|
|
args.method = (VasEBoot_ieee1275_cell_t) "size";
|
|
args.ihandle = dev_ihandle;
|
|
args.result = 1;
|
|
|
|
if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.result))
|
|
{
|
|
VasEBoot_error (VAS_EBOOT_ERR_UNKNOWN_DEVICE, "can't get disk size");
|
|
goto fail;
|
|
}
|
|
|
|
disk->total_sectors = args.size1;
|
|
disk->total_sectors <<= 32;
|
|
disk->total_sectors += args.size2;
|
|
disk->total_sectors >>= VAS_EBOOT_DISK_SECTOR_BITS;
|
|
|
|
disk->id = dev_ihandle;
|
|
|
|
disk->data = data;
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
if (dev_ihandle)
|
|
VasEBoot_ieee1275_close (dev_ihandle);
|
|
VasEBoot_free (data);
|
|
return VasEBoot_errno;
|
|
}
|
|
|
|
static void
|
|
VasEBoot_nand_close (VasEBoot_disk_t disk)
|
|
{
|
|
VasEBoot_ieee1275_close (((struct VasEBoot_nand_data *) disk->data)->handle);
|
|
VasEBoot_free (disk->data);
|
|
}
|
|
|
|
static VasEBoot_err_t
|
|
VasEBoot_nand_read (VasEBoot_disk_t disk, VasEBoot_disk_addr_t sector,
|
|
VasEBoot_size_t size, char *buf)
|
|
{
|
|
struct VasEBoot_nand_data *data = disk->data;
|
|
VasEBoot_size_t bsize, ofs;
|
|
|
|
struct read_args
|
|
{
|
|
struct VasEBoot_ieee1275_common_hdr common;
|
|
VasEBoot_ieee1275_cell_t method;
|
|
VasEBoot_ieee1275_cell_t ihandle;
|
|
VasEBoot_ieee1275_cell_t ofs;
|
|
VasEBoot_ieee1275_cell_t page;
|
|
VasEBoot_ieee1275_cell_t len;
|
|
VasEBoot_ieee1275_cell_t buf;
|
|
VasEBoot_ieee1275_cell_t result;
|
|
} args;
|
|
|
|
INIT_IEEE1275_COMMON (&args.common, "call-method", 6, 1);
|
|
args.method = (VasEBoot_ieee1275_cell_t) "pio-read";
|
|
args.ihandle = data->handle;
|
|
args.buf = (VasEBoot_ieee1275_cell_t) buf;
|
|
args.page = (VasEBoot_ieee1275_cell_t) ((VasEBoot_size_t) sector / data->block_size);
|
|
|
|
ofs = ((VasEBoot_size_t) sector % data->block_size) << VAS_EBOOT_DISK_SECTOR_BITS;
|
|
size <<= VAS_EBOOT_DISK_SECTOR_BITS;
|
|
bsize = (data->block_size << VAS_EBOOT_DISK_SECTOR_BITS);
|
|
|
|
do
|
|
{
|
|
VasEBoot_size_t len;
|
|
|
|
len = (ofs + size > bsize) ? (bsize - ofs) : size;
|
|
|
|
args.len = (VasEBoot_ieee1275_cell_t) len;
|
|
args.ofs = (VasEBoot_ieee1275_cell_t) ofs;
|
|
args.result = 1;
|
|
|
|
if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.result))
|
|
return VasEBoot_error (VAS_EBOOT_ERR_READ_ERROR, N_("failure reading sector 0x%llx "
|
|
"from `%s'"),
|
|
(unsigned long long) sector,
|
|
disk->name);
|
|
|
|
ofs = 0;
|
|
size -= len;
|
|
args.buf += len;
|
|
args.page++;
|
|
} while (size);
|
|
|
|
return VAS_EBOOT_ERR_NONE;
|
|
}
|
|
|
|
static VasEBoot_err_t
|
|
VasEBoot_nand_write (VasEBoot_disk_t disk __attribute ((unused)),
|
|
VasEBoot_disk_addr_t sector __attribute ((unused)),
|
|
VasEBoot_size_t size __attribute ((unused)),
|
|
const char *buf __attribute ((unused)))
|
|
{
|
|
return VasEBoot_error (VAS_EBOOT_ERR_NOT_IMPLEMENTED_YET,
|
|
"nand write is not supported");
|
|
}
|
|
|
|
static struct VasEBoot_disk_dev VasEBoot_nand_dev =
|
|
{
|
|
.name = "nand",
|
|
.id = VAS_EBOOT_DISK_DEVICE_NAND_ID,
|
|
.disk_iterate = VasEBoot_nand_iterate,
|
|
.disk_open = VasEBoot_nand_open,
|
|
.disk_close = VasEBoot_nand_close,
|
|
.disk_read = VasEBoot_nand_read,
|
|
.disk_write = VasEBoot_nand_write,
|
|
.next = 0
|
|
};
|
|
|
|
VAS_EBOOT_MOD_INIT(nand)
|
|
{
|
|
VasEBoot_disk_dev_register (&VasEBoot_nand_dev);
|
|
}
|
|
|
|
VAS_EBOOT_MOD_FINI(nand)
|
|
{
|
|
VasEBoot_disk_dev_unregister (&VasEBoot_nand_dev);
|
|
}
|