1
/* nand.c - NAND flash disk access. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 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>
24
#include <grub/ieee1275/ieee1275.h>
28
grub_ieee1275_ihandle_t handle;
29
grub_uint32_t block_size;
33
grub_nand_iterate (int (*hook) (const char *name))
35
auto int dev_iterate (struct grub_ieee1275_devalias *alias);
36
int dev_iterate (struct grub_ieee1275_devalias *alias)
38
if (! grub_strcmp (alias->name, "nand"))
47
return grub_devalias_iterate (dev_iterate);
51
grub_nand_read (grub_disk_t disk, grub_disk_addr_t sector,
52
grub_size_t size, char *buf);
55
grub_nand_open (const char *name, grub_disk_t disk)
57
grub_ieee1275_ihandle_t dev_ihandle = 0;
58
struct grub_nand_data *data = 0;
61
struct grub_ieee1275_common_hdr common;
62
grub_ieee1275_cell_t method;
63
grub_ieee1275_cell_t ihandle;
64
grub_ieee1275_cell_t result;
65
grub_ieee1275_cell_t size1;
66
grub_ieee1275_cell_t size2;
69
if (! grub_strstr (name, "nand"))
70
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a NAND device");
72
data = grub_malloc (sizeof (*data));
76
grub_ieee1275_open (name, &dev_ihandle);
79
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
83
data->handle = dev_ihandle;
85
INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 2);
86
args.method = (grub_ieee1275_cell_t) "block-size";
87
args.ihandle = dev_ihandle;
90
if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.result))
92
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't get block size");
96
data->block_size = (args.size1 >> GRUB_DISK_SECTOR_BITS);
98
INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 3);
99
args.method = (grub_ieee1275_cell_t) "size";
100
args.ihandle = dev_ihandle;
103
if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.result))
105
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't get disk size");
109
disk->total_sectors = args.size1;
110
disk->total_sectors <<= 32;
111
disk->total_sectors += args.size2;
112
disk->total_sectors >>= GRUB_DISK_SECTOR_BITS;
114
disk->id = dev_ihandle;
122
grub_ieee1275_close (dev_ihandle);
128
grub_nand_close (grub_disk_t disk)
130
grub_ieee1275_close (((struct grub_nand_data *) disk->data)->handle);
131
grub_free (disk->data);
135
grub_nand_read (grub_disk_t disk, grub_disk_addr_t sector,
136
grub_size_t size, char *buf)
138
struct grub_nand_data *data = disk->data;
139
grub_size_t bsize, ofs;
143
struct grub_ieee1275_common_hdr common;
144
grub_ieee1275_cell_t method;
145
grub_ieee1275_cell_t ihandle;
146
grub_ieee1275_cell_t ofs;
147
grub_ieee1275_cell_t page;
148
grub_ieee1275_cell_t len;
149
grub_ieee1275_cell_t buf;
150
grub_ieee1275_cell_t result;
153
INIT_IEEE1275_COMMON (&args.common, "call-method", 6, 1);
154
args.method = (grub_ieee1275_cell_t) "pio-read";
155
args.ihandle = data->handle;
156
args.buf = (grub_ieee1275_cell_t) buf;
157
args.page = (grub_ieee1275_cell_t) ((grub_size_t) sector / data->block_size);
159
ofs = ((grub_size_t) sector % data->block_size) << GRUB_DISK_SECTOR_BITS;
160
size <<= GRUB_DISK_SECTOR_BITS;
161
bsize = (data->block_size << GRUB_DISK_SECTOR_BITS);
167
len = (ofs + size > bsize) ? (bsize - ofs) : size;
169
args.len = (grub_ieee1275_cell_t) len;
170
args.ofs = (grub_ieee1275_cell_t) ofs;
173
if ((IEEE1275_CALL_ENTRY_FN (&args) == -1) || (args.result))
174
return grub_error (GRUB_ERR_READ_ERROR, "read error");
182
return GRUB_ERR_NONE;
186
grub_nand_write (grub_disk_t disk __attribute ((unused)),
187
grub_disk_addr_t sector __attribute ((unused)),
188
grub_size_t size __attribute ((unused)),
189
const char *buf __attribute ((unused)))
191
return GRUB_ERR_NOT_IMPLEMENTED_YET;
194
static struct grub_disk_dev grub_nand_dev =
197
.id = GRUB_DISK_DEVICE_NAND_ID,
198
.iterate = grub_nand_iterate,
199
.open = grub_nand_open,
200
.close = grub_nand_close,
201
.read = grub_nand_read,
202
.write = grub_nand_write,
208
grub_disk_dev_register (&grub_nand_dev);
213
grub_disk_dev_unregister (&grub_nand_dev);