]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/powerpc/ps3/ps3disk.c
Re-sync loader.mk and ficl.mk to where they should be
[FreeBSD/FreeBSD.git] / stand / powerpc / ps3 / ps3disk.c
1 /*-
2  * Copyright (C) 2008 Semihalf, Rafal Jaworowski
3  * Copyright (C) 2009 Semihalf, Piotr Ziecik
4  * Copyright (C) 2011 glevand (geoffrey.levand@mail.ru)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
26  *
27  * $FreeBSD$
28  */
29
30 #include <sys/endian.h>
31 #include <machine/stdarg.h>
32 #include <stand.h>
33 #include <uuid.h>
34
35 #define FSTYPENAMES
36 #include <sys/disklabel.h>
37 #include <sys/diskmbr.h>
38 #include <sys/gpt.h>
39
40 #include "bootstrap.h"
41 #include "ps3bus.h"
42 #include "ps3devdesc.h"
43 #include "ps3stor.h"
44
45 #define dev_printf(dev, fmt, args...)   \
46         printf("%s%d: " fmt "\n" , dev->d_dev->dv_name, dev->d_unit, ##args)
47
48 #ifdef DISK_DEBUG
49 #define DEBUG(fmt, args...)             printf("%s:%d: " fmt "\n" , __func__ , __LINE__, ##args)
50 #else
51 #define DEBUG(fmt, args...)
52 #endif
53
54 struct open_dev;
55
56 static int ps3disk_open_gpt(struct ps3_devdesc *dev, struct open_dev *od);
57 static void ps3disk_uuid_letoh(uuid_t *uuid);
58
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 int ps3disk_print(int verbose);
65
66 struct devsw ps3disk = {
67         "disk",
68         DEVT_DISK,
69         ps3disk_init,
70         ps3disk_strategy,
71         ps3disk_open,
72         ps3disk_close,
73         noioctl,
74         ps3disk_print,
75 };
76
77 struct gpt_part {
78         int             gp_index;
79         uuid_t          gp_type;
80         uint64_t        gp_start;
81         uint64_t        gp_end;
82 };
83
84 struct open_dev {
85         uint64_t od_start;
86
87         union {
88                 struct {
89                         int             nparts;
90                         struct gpt_part *parts;
91                 } gpt;
92         } od_kind;
93 };
94
95 #define od_gpt_nparts   od_kind.gpt.nparts
96 #define od_gpt_parts    od_kind.gpt.parts
97
98 static struct ps3_stordev stor_dev;
99
100 static int ps3disk_init(void)
101 {
102         int err;
103
104         err = ps3stor_setup(&stor_dev, PS3_DEV_TYPE_STOR_DISK);
105         if (err)
106                 return err;
107
108         return 0;
109 }
110
111 static int ps3disk_strategy(void *devdata, int flag, daddr_t dblk,
112     size_t size, char *buf, size_t *rsize)
113 {
114         struct ps3_devdesc *dev = (struct ps3_devdesc *) devdata;
115         struct open_dev *od = (struct open_dev *) dev->d_disk.data;
116         int err;
117
118         flag &= F_MASK;
119         if (flag != F_READ) {
120                 dev_printf(dev, "write operation is not supported!\n");
121                 return EROFS;
122         }
123
124         if (size % stor_dev.sd_blksize) {
125                 dev_printf(dev, "size=%u is not multiple of device block size=%llu\n",
126                         size, stor_dev.sd_blksize);
127                 return EIO;
128         }
129
130         if (rsize)
131                 *rsize = 0;
132
133         err = ps3stor_read_sectors(&stor_dev, dev->d_unit, od->od_start + dblk,
134                 size / stor_dev.sd_blksize,  0, buf);
135
136         if (!err && rsize)
137                 *rsize = size;
138
139         if (err)
140                 dev_printf(dev, "read operation failed dblk=%llu size=%d err=%d\n",
141                         dblk, size, err);
142
143         return err;
144 }
145
146 static int ps3disk_open(struct open_file *f, ...)
147 {
148         va_list ap;
149         struct ps3_devdesc *dev;
150         struct open_dev *od;
151         int err;
152
153         va_start(ap, f);
154         dev = va_arg(ap, struct ps3_devdesc *);
155         va_end(ap);
156
157         od = malloc(sizeof(struct open_dev));
158         if (!od) {
159                 dev_printf(dev, "couldn't allocate memory for new open_dev\n");
160                 return ENOMEM;
161         }
162
163         err = ps3disk_open_gpt(dev, od);
164
165         if (err) {
166                 dev_printf(dev, "couldn't open GPT disk error=%d\n", err);
167                 free(od);
168         } else {
169                 ((struct ps3_devdesc *) (f->f_devdata))->d_disk.data = od;
170         }
171
172         return err;
173 }
174
175 static int ps3disk_close(struct open_file *f)
176 {
177         struct ps3_devdesc *dev = f->f_devdata;
178         struct open_dev *od = dev->d_disk.data;
179
180         if (dev->d_disk.ptype == PTYPE_GPT && od->od_gpt_nparts)
181                 free(od->od_gpt_parts);
182
183         free(od);
184
185         dev->d_disk.data = NULL;
186
187         return 0;
188 }
189
190 static int ps3disk_print(int verbose)
191 {
192         return (0);
193 }
194
195 static int ps3disk_open_gpt(struct ps3_devdesc *dev, struct open_dev *od)
196 {
197         char buf[512];
198         struct gpt_hdr *hdr;
199         struct gpt_ent *ent;
200         daddr_t slba, elba, lba;
201         int nparts, eps, i, part, err;
202
203         od->od_gpt_nparts = 0;
204         od->od_gpt_parts = NULL;
205
206         err = ps3stor_read_sectors(&stor_dev, dev->d_unit, 0, 1, 0, buf);
207         if (err) {
208                 err = EIO;
209                 goto out;
210         }
211
212         if (le16toh(*((uint16_t *) (buf + DOSMAGICOFFSET))) != DOSMAGIC) {
213                 err = ENXIO;
214                 goto out;
215         }
216
217         err = ps3stor_read_sectors(&stor_dev, dev->d_unit, 1, 1, 0, buf);
218         if (err) {
219                 err = EIO;
220                 goto out;
221         }
222
223         hdr = (struct gpt_hdr *) buf;
224
225         if (bcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) ||
226                 le64toh(hdr->hdr_lba_self) != 1 || le32toh(hdr->hdr_revision) < 0x00010000 ||
227                 le32toh(hdr->hdr_entsz) < sizeof(struct gpt_ent) ||
228                 stor_dev.sd_blksize % le32toh(hdr->hdr_entsz) != 0) {
229                 err = ENXIO;
230                 goto out;
231         }
232
233         nparts = 0;
234         eps = stor_dev.sd_blksize / le32toh(hdr->hdr_entsz);
235         slba = le64toh(hdr->hdr_lba_table);
236         elba = slba + le32toh(hdr->hdr_entries) / eps;
237
238         for (lba = slba; lba < elba; lba++) {
239                 err = ps3stor_read_sectors(&stor_dev, dev->d_unit, lba, 1, 0, buf);
240                 if (err) {
241                         err = EIO;
242                         goto out;
243                 }
244
245                 ent = (struct gpt_ent *) buf;
246
247                 for (i = 0; i < eps; i++) {
248                         if (uuid_is_nil(&ent[i].ent_type, NULL) ||
249                                 le64toh(ent[i].ent_lba_start) == 0 ||
250                                 le64toh(ent[i].ent_lba_end) < le64toh(ent[i].ent_lba_start))
251                                 continue;
252
253                         nparts++;
254                 }
255         }
256
257         if (nparts) {
258                 od->od_gpt_nparts = nparts;
259
260                 od->od_gpt_parts = malloc(nparts * sizeof(struct gpt_part));
261                 if (!od->od_gpt_parts) {
262                         err = ENOMEM;
263                         goto out;
264                 }
265
266                 for (lba = slba, part = 0; lba < elba; lba++) {
267                         err = ps3stor_read_sectors(&stor_dev, dev->d_unit, lba, 1, 0, buf);
268                         if (err) {
269                                 err = EIO;
270                                 goto out;
271                         }
272
273                         ent = (struct gpt_ent *) buf;
274
275                         for (i = 0; i < eps; i++) {
276                                 if (uuid_is_nil(&ent[i].ent_type, NULL) ||
277                                         le64toh(ent[i].ent_lba_start) == 0 ||
278                                         le64toh(ent[i].ent_lba_end) < le64toh(ent[i].ent_lba_start))
279                                         continue;
280
281                                 od->od_gpt_parts[part].gp_index = (lba - slba) * eps + i + 1;
282                                 od->od_gpt_parts[part].gp_type = ent[i].ent_type;
283                                 od->od_gpt_parts[part].gp_start = le64toh(ent[i].ent_lba_start);
284                                 od->od_gpt_parts[part].gp_end = le64toh(ent[i].ent_lba_end);
285                                 ps3disk_uuid_letoh(&od->od_gpt_parts[part].gp_type);
286                                 part++;
287                         }
288                 }
289         }
290
291         dev->d_disk.ptype = PTYPE_GPT;
292
293         if (od->od_gpt_nparts && !dev->d_disk.pnum)
294                 dev->d_disk.pnum = od->od_gpt_parts[0].gp_index;
295
296         for (i = 0; i < od->od_gpt_nparts; i++)
297                 if (od->od_gpt_parts[i].gp_index == dev->d_disk.pnum)
298                         od->od_start = od->od_gpt_parts[i].gp_start;
299
300         err = 0;
301
302 out:
303
304         if (err && od->od_gpt_parts)
305                 free(od->od_gpt_parts);
306
307         return err;
308 }
309
310 static void ps3disk_uuid_letoh(uuid_t *uuid)
311 {
312         uuid->time_low = le32toh(uuid->time_low);
313         uuid->time_mid = le16toh(uuid->time_mid);
314         uuid->time_hi_and_version = le16toh(uuid->time_hi_and_version);
315 }