2 * Copyright (C) 2008 Semihalf, Rafal Jaworowski
3 * Copyright (C) 2009 Semihalf, Piotr Ziecik
4 * Copyright (C) 2011 glevand (geoffrey.levand@mail.ru)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/endian.h>
31 #include <machine/stdarg.h>
36 #include <sys/disklabel.h>
37 #include <sys/diskmbr.h>
40 #include "bootstrap.h"
42 #include "ps3devdesc.h"
45 #define dev_printf(dev, fmt, args...) \
46 printf("%s%d: " fmt "\n" , dev->d_dev->dv_name, dev->d_unit, ##args)
49 #define DEBUG(fmt, args...) printf("%s:%d: " fmt "\n" , __func__ , __LINE__, ##args)
51 #define DEBUG(fmt, args...)
56 static int ps3disk_open_gpt(struct ps3_devdesc *dev, struct open_dev *od);
57 static void ps3disk_uuid_letoh(uuid_t *uuid);
59 static int ps3disk_init(void);
60 static int ps3disk_strategy(void *devdata, int flag, daddr_t dblk,
61 size_t size, char *buf, size_t *rsize);
62 static int ps3disk_open(struct open_file *f, ...);
63 static int ps3disk_close(struct open_file *f);
64 static void ps3disk_print(int verbose);
66 struct devsw ps3disk = {
90 struct gpt_part *parts;
95 #define od_gpt_nparts od_kind.gpt.nparts
96 #define od_gpt_parts od_kind.gpt.parts
98 static struct ps3_stordev stor_dev;
100 static int ps3disk_init(void)
104 err = ps3stor_setup(&stor_dev, PS3_DEV_TYPE_STOR_DISK);
111 static int ps3disk_strategy(void *devdata, int flag, daddr_t dblk,
112 size_t size, char *buf, size_t *rsize)
114 struct ps3_devdesc *dev = (struct ps3_devdesc *) devdata;
115 struct open_dev *od = (struct open_dev *) dev->d_disk.data;
118 if (flag != F_READ) {
119 dev_printf(dev, "write operation is not supported!\n");
123 if (size % stor_dev.sd_blksize) {
124 dev_printf(dev, "size=%u is not multiple of device block size=%llu\n",
125 size, stor_dev.sd_blksize);
132 err = ps3stor_read_sectors(&stor_dev, dev->d_unit, od->od_start + dblk,
133 size / stor_dev.sd_blksize, 0, buf);
139 dev_printf(dev, "read operation failed dblk=%llu size=%d err=%d\n",
145 static int ps3disk_open(struct open_file *f, ...)
148 struct ps3_devdesc *dev;
153 dev = va_arg(ap, struct ps3_devdesc *);
156 od = malloc(sizeof(struct open_dev));
158 dev_printf(dev, "couldn't allocate memory for new open_dev\n");
162 err = ps3disk_open_gpt(dev, od);
165 dev_printf(dev, "couldn't open GPT disk error=%d\n", err);
168 ((struct ps3_devdesc *) (f->f_devdata))->d_disk.data = od;
174 static int ps3disk_close(struct open_file *f)
176 struct ps3_devdesc *dev = f->f_devdata;
177 struct open_dev *od = dev->d_disk.data;
179 if (dev->d_disk.ptype == PTYPE_GPT && od->od_gpt_nparts)
180 free(od->od_gpt_parts);
184 dev->d_disk.data = NULL;
189 static void ps3disk_print(int verbose)
193 static int ps3disk_open_gpt(struct ps3_devdesc *dev, struct open_dev *od)
198 daddr_t slba, elba, lba;
199 int nparts, eps, i, part, err;
201 od->od_gpt_nparts = 0;
202 od->od_gpt_parts = NULL;
204 err = ps3stor_read_sectors(&stor_dev, dev->d_unit, 0, 1, 0, buf);
210 if (le16toh(*((uint16_t *) (buf + DOSMAGICOFFSET))) != DOSMAGIC) {
215 err = ps3stor_read_sectors(&stor_dev, dev->d_unit, 1, 1, 0, buf);
221 hdr = (struct gpt_hdr *) buf;
223 if (bcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) ||
224 le64toh(hdr->hdr_lba_self) != 1 || le32toh(hdr->hdr_revision) < 0x00010000 ||
225 le32toh(hdr->hdr_entsz) < sizeof(struct gpt_ent) ||
226 stor_dev.sd_blksize % le32toh(hdr->hdr_entsz) != 0) {
232 eps = stor_dev.sd_blksize / le32toh(hdr->hdr_entsz);
233 slba = le64toh(hdr->hdr_lba_table);
234 elba = slba + le32toh(hdr->hdr_entries) / eps;
236 for (lba = slba; lba < elba; lba++) {
237 err = ps3stor_read_sectors(&stor_dev, dev->d_unit, lba, 1, 0, buf);
243 ent = (struct gpt_ent *) buf;
245 for (i = 0; i < eps; i++) {
246 if (uuid_is_nil(&ent[i].ent_type, NULL) ||
247 le64toh(ent[i].ent_lba_start) == 0 ||
248 le64toh(ent[i].ent_lba_end) < le64toh(ent[i].ent_lba_start))
256 od->od_gpt_nparts = nparts;
258 od->od_gpt_parts = malloc(nparts * sizeof(struct gpt_part));
259 if (!od->od_gpt_parts) {
264 for (lba = slba, part = 0; lba < elba; lba++) {
265 err = ps3stor_read_sectors(&stor_dev, dev->d_unit, lba, 1, 0, buf);
271 ent = (struct gpt_ent *) buf;
273 for (i = 0; i < eps; i++) {
274 if (uuid_is_nil(&ent[i].ent_type, NULL) ||
275 le64toh(ent[i].ent_lba_start) == 0 ||
276 le64toh(ent[i].ent_lba_end) < le64toh(ent[i].ent_lba_start))
279 od->od_gpt_parts[part].gp_index = (lba - slba) * eps + i + 1;
280 od->od_gpt_parts[part].gp_type = ent[i].ent_type;
281 od->od_gpt_parts[part].gp_start = le64toh(ent[i].ent_lba_start);
282 od->od_gpt_parts[part].gp_end = le64toh(ent[i].ent_lba_end);
283 ps3disk_uuid_letoh(&od->od_gpt_parts[part].gp_type);
289 dev->d_disk.ptype = PTYPE_GPT;
291 if (od->od_gpt_nparts && !dev->d_disk.pnum)
292 dev->d_disk.pnum = od->od_gpt_parts[0].gp_index;
294 for (i = 0; i < od->od_gpt_nparts; i++)
295 if (od->od_gpt_parts[i].gp_index == dev->d_disk.pnum)
296 od->od_start = od->od_gpt_parts[i].gp_start;
302 if (err && od->od_gpt_parts)
303 free(od->od_gpt_parts);
308 static void ps3disk_uuid_letoh(uuid_t *uuid)
310 uuid->time_low = le32toh(uuid->time_low);
311 uuid->time_mid = le16toh(uuid->time_mid);
312 uuid->time_hi_and_version = le16toh(uuid->time_hi_and_version);