1201941Smarcel/*-
2201941Smarcel * Copyright (c) 2010 Marcel Moolenaar
3201941Smarcel * All rights reserved.
4201941Smarcel *
5201941Smarcel * Redistribution and use in source and binary forms, with or without
6201941Smarcel * modification, are permitted provided that the following conditions
7201941Smarcel * are met:
8201941Smarcel * 1. Redistributions of source code must retain the above copyright
9201941Smarcel *    notice, this list of conditions and the following disclaimer.
10201941Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11201941Smarcel *    notice, this list of conditions and the following disclaimer in the
12201941Smarcel *    documentation and/or other materials provided with the distribution.
13201941Smarcel *
14201941Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15201941Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16201941Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17201941Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18201941Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19201941Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20201941Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21201941Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22201941Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23201941Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24201941Smarcel * SUCH DAMAGE.
25201941Smarcel */
26201941Smarcel
27201941Smarcel#include <sys/cdefs.h>
28201941Smarcel__FBSDID("$FreeBSD$");
29201941Smarcel
30201941Smarcel#include <sys/param.h>
31201941Smarcel#include <sys/time.h>
32201941Smarcel#include <stddef.h>
33201941Smarcel#include <stdarg.h>
34201941Smarcel
35201941Smarcel#include <bootstrap.h>
36201941Smarcel
37201941Smarcel#include <efi.h>
38201941Smarcel#include <efilib.h>
39201941Smarcel#include <efiprot.h>
40201941Smarcel
41201941Smarcelstatic EFI_GUID blkio_guid = BLOCK_IO_PROTOCOL;
42271135Semastestatic EFI_GUID devpath_guid = DEVICE_PATH_PROTOCOL;
43201941Smarcel
44201941Smarcelstatic int efipart_init(void);
45201941Smarcelstatic int efipart_strategy(void *, int, daddr_t, size_t, char *, size_t *);
46201941Smarcelstatic int efipart_open(struct open_file *, ...);
47201941Smarcelstatic int efipart_close(struct open_file *);
48201941Smarcelstatic void efipart_print(int);
49201941Smarcel
50201941Smarcelstruct devsw efipart_dev = {
51201941Smarcel	.dv_name = "part",
52201941Smarcel	.dv_type = DEVT_DISK,
53201941Smarcel	.dv_init = efipart_init,
54201941Smarcel	.dv_strategy = efipart_strategy,
55201941Smarcel	.dv_open = efipart_open,
56201941Smarcel	.dv_close = efipart_close,
57201941Smarcel	.dv_ioctl = noioctl,
58201941Smarcel	.dv_print = efipart_print,
59201941Smarcel	.dv_cleanup = NULL
60201941Smarcel};
61201941Smarcel
62201941Smarcelstatic int
63201941Smarcelefipart_init(void)
64201941Smarcel{
65201941Smarcel	EFI_BLOCK_IO *blkio;
66271135Semaste	EFI_DEVICE_PATH *devpath, *node;
67271135Semaste	EFI_HANDLE *hin, *hout, *aliases, handle;
68201941Smarcel	EFI_STATUS status;
69201941Smarcel	UINTN sz;
70271135Semaste	CHAR16 *path;
71201941Smarcel	u_int n, nin, nout;
72201941Smarcel	int err;
73201941Smarcel
74201941Smarcel	sz = 0;
75217067Smarcel	hin = NULL;
76201941Smarcel	status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, 0);
77201941Smarcel	if (status == EFI_BUFFER_TOO_SMALL) {
78271135Semaste		hin = (EFI_HANDLE *)malloc(sz * 3);
79201941Smarcel		status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz,
80201941Smarcel		    hin);
81201941Smarcel		if (EFI_ERROR(status))
82201941Smarcel			free(hin);
83201941Smarcel	}
84201941Smarcel	if (EFI_ERROR(status))
85201941Smarcel		return (efi_status_to_errno(status));
86201941Smarcel
87201941Smarcel	/* Filter handles to only include FreeBSD partitions. */
88201941Smarcel	nin = sz / sizeof(EFI_HANDLE);
89201941Smarcel	hout = hin + nin;
90271135Semaste	aliases = hout + nin;
91201941Smarcel	nout = 0;
92201941Smarcel
93271135Semaste	bzero(aliases, nin * sizeof(EFI_HANDLE));
94271135Semaste
95201941Smarcel	for (n = 0; n < nin; n++) {
96271135Semaste		status = BS->HandleProtocol(hin[n], &devpath_guid,
97271135Semaste		    (void **)&devpath);
98271135Semaste		if (EFI_ERROR(status)) {
99271135Semaste			continue;
100271135Semaste		}
101271135Semaste		node = devpath;
102271135Semaste		while (!IsDevicePathEnd(NextDevicePathNode(node)))
103271135Semaste			node = NextDevicePathNode(node);
104271135Semaste		status = BS->HandleProtocol(hin[n], &blkio_guid,
105271135Semaste		    (void**)&blkio);
106201941Smarcel		if (EFI_ERROR(status))
107201941Smarcel			continue;
108201941Smarcel		if (!blkio->Media->LogicalPartition)
109201941Smarcel			continue;
110271135Semaste
111271135Semaste		/*
112271135Semaste		 * If we come across a logical partition of subtype CDROM
113271135Semaste		 * it doesn't refer to the CD filesystem itself, but rather
114271135Semaste		 * to any usable El Torito boot image on it. In this case
115271135Semaste		 * we try to find the parent device and add that instead as
116271135Semaste		 * that will be the CD filesystem.
117271135Semaste		 */
118271135Semaste		if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
119271135Semaste		    DevicePathSubType(node) == MEDIA_CDROM_DP) {
120271135Semaste			node->Type = END_DEVICE_PATH_TYPE;
121271135Semaste			node->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
122271135Semaste			status = BS->LocateDevicePath(&blkio_guid, &devpath,
123271135Semaste			    &handle);
124271135Semaste			if (EFI_ERROR(status))
125271135Semaste				continue;
126271135Semaste			hout[nout] = handle;
127271135Semaste			aliases[nout] = hin[n];
128271135Semaste		} else
129271135Semaste			hout[nout] = hin[n];
130201941Smarcel		nout++;
131201941Smarcel	}
132201941Smarcel
133271135Semaste	err = efi_register_handles(&efipart_dev, hout, aliases, nout);
134201941Smarcel	free(hin);
135201941Smarcel	return (err);
136201941Smarcel}
137201941Smarcel
138201941Smarcelstatic void
139201941Smarcelefipart_print(int verbose)
140201941Smarcel{
141201941Smarcel	char line[80];
142201941Smarcel	EFI_BLOCK_IO *blkio;
143201941Smarcel	EFI_HANDLE h;
144201941Smarcel	EFI_STATUS status;
145201941Smarcel	u_int unit;
146201941Smarcel
147201941Smarcel	for (unit = 0, h = efi_find_handle(&efipart_dev, 0);
148201941Smarcel	    h != NULL; h = efi_find_handle(&efipart_dev, ++unit)) {
149201941Smarcel		sprintf(line, "    %s%d:", efipart_dev.dv_name, unit);
150201941Smarcel		pager_output(line);
151201941Smarcel
152271135Semaste		status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
153201941Smarcel		if (!EFI_ERROR(status)) {
154201941Smarcel			sprintf(line, "    %llu blocks",
155201941Smarcel			    (unsigned long long)(blkio->Media->LastBlock + 1));
156201941Smarcel			pager_output(line);
157201941Smarcel			if (blkio->Media->RemovableMedia)
158201941Smarcel				pager_output(" (removable)");
159201941Smarcel		}
160201941Smarcel		pager_output("\n");
161201941Smarcel	}
162201941Smarcel}
163201941Smarcel
164201941Smarcelstatic int
165201941Smarcelefipart_open(struct open_file *f, ...)
166201941Smarcel{
167201941Smarcel	va_list args;
168201941Smarcel	struct devdesc *dev;
169201941Smarcel	EFI_BLOCK_IO *blkio;
170201941Smarcel	EFI_HANDLE h;
171201941Smarcel	EFI_STATUS status;
172201941Smarcel
173201941Smarcel	va_start(args, f);
174201941Smarcel	dev = va_arg(args, struct devdesc*);
175201941Smarcel	va_end(args);
176201941Smarcel
177201941Smarcel	h = efi_find_handle(&efipart_dev, dev->d_unit);
178201941Smarcel	if (h == NULL)
179201941Smarcel		return (EINVAL);
180201941Smarcel
181271135Semaste	status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
182201941Smarcel	if (EFI_ERROR(status))
183201941Smarcel		return (efi_status_to_errno(status));
184201941Smarcel
185201941Smarcel	if (!blkio->Media->MediaPresent)
186201941Smarcel		return (EAGAIN);
187201941Smarcel
188201941Smarcel	dev->d_opendata = blkio;
189201941Smarcel	return (0);
190201941Smarcel}
191201941Smarcel
192201941Smarcelstatic int
193201941Smarcelefipart_close(struct open_file *f)
194201941Smarcel{
195201941Smarcel	struct devdesc *dev;
196201941Smarcel
197201941Smarcel	dev = (struct devdesc *)(f->f_devdata);
198201941Smarcel	if (dev->d_opendata == NULL)
199201941Smarcel		return (EINVAL);
200201941Smarcel
201201941Smarcel	dev->d_opendata = NULL;
202201941Smarcel	return (0);
203201941Smarcel}
204201941Smarcel
205201941Smarcel/*
206201941Smarcel * efipart_readwrite()
207201941Smarcel * Internal equivalent of efipart_strategy(), which operates on the
208201941Smarcel * media-native block size. This function expects all I/O requests
209201941Smarcel * to be within the media size and returns an error if such is not
210201941Smarcel * the case.
211201941Smarcel */
212201941Smarcelstatic int
213201941Smarcelefipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
214201941Smarcel    char *buf)
215201941Smarcel{
216201941Smarcel	EFI_STATUS status;
217201941Smarcel
218201941Smarcel	if (blkio == NULL)
219201941Smarcel		return (ENXIO);
220201941Smarcel	if (blk < 0 || blk > blkio->Media->LastBlock)
221201941Smarcel		return (EIO);
222201941Smarcel	if ((blk + nblks - 1) > blkio->Media->LastBlock)
223201941Smarcel		return (EIO);
224201941Smarcel
225201941Smarcel	switch (rw) {
226201941Smarcel	case F_READ:
227201941Smarcel		status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
228201941Smarcel		    nblks * blkio->Media->BlockSize, buf);
229201941Smarcel		break;
230201941Smarcel	case F_WRITE:
231201941Smarcel		if (blkio->Media->ReadOnly)
232201941Smarcel			return (EROFS);
233201941Smarcel		status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
234201941Smarcel		    nblks * blkio->Media->BlockSize, buf);
235201941Smarcel		break;
236201941Smarcel	default:
237201941Smarcel		return (ENOSYS);
238201941Smarcel	}
239201941Smarcel
240201941Smarcel	if (EFI_ERROR(status))
241219683Smarcel		printf("%s: rw=%d, status=%lu\n", __func__, rw, (u_long)status);
242201941Smarcel	return (efi_status_to_errno(status));
243201941Smarcel}
244201941Smarcel
245201941Smarcelstatic int
246201941Smarcelefipart_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
247201941Smarcel    size_t *rsize)
248201941Smarcel{
249201941Smarcel	struct devdesc *dev = (struct devdesc *)devdata;
250201941Smarcel	EFI_BLOCK_IO *blkio;
251201941Smarcel	off_t off;
252201941Smarcel	char *blkbuf;
253201941Smarcel	size_t blkoff, blksz;
254201941Smarcel	int error;
255201941Smarcel
256201941Smarcel	if (dev == NULL || blk < 0)
257201941Smarcel		return (EINVAL);
258201941Smarcel
259201941Smarcel	blkio = dev->d_opendata;
260201941Smarcel	if (blkio == NULL)
261201941Smarcel		return (ENXIO);
262201941Smarcel
263201941Smarcel	if (size == 0 || (size % 512) != 0)
264201941Smarcel		return (EIO);
265201941Smarcel
266201941Smarcel	if (rsize != NULL)
267201941Smarcel		*rsize = size;
268201941Smarcel
269201941Smarcel	if (blkio->Media->BlockSize == 512)
270201941Smarcel		return (efipart_readwrite(blkio, rw, blk, size / 512, buf));
271201941Smarcel
272201941Smarcel	/*
273201941Smarcel	 * The block size of the media is not 512B per sector.
274201941Smarcel	 */
275201941Smarcel	blkbuf = malloc(blkio->Media->BlockSize);
276201941Smarcel	if (blkbuf == NULL)
277201941Smarcel		return (ENOMEM);
278201941Smarcel
279201941Smarcel	error = 0;
280201941Smarcel	off = blk * 512;
281201941Smarcel	blk = off / blkio->Media->BlockSize;
282201941Smarcel	blkoff = off % blkio->Media->BlockSize;
283201941Smarcel	blksz = blkio->Media->BlockSize - blkoff;
284201941Smarcel	while (size > 0) {
285201941Smarcel		error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
286201941Smarcel		if (error)
287201941Smarcel			break;
288201941Smarcel		if (size < blksz)
289201941Smarcel			blksz = size;
290201941Smarcel		bcopy(blkbuf + blkoff, buf, blksz);
291201941Smarcel		buf += blksz;
292201941Smarcel		size -= blksz;
293201941Smarcel		blk++;
294201941Smarcel		blkoff = 0;
295201941Smarcel		blksz = blkio->Media->BlockSize;
296201941Smarcel	}
297201941Smarcel
298201941Smarcel	free(blkbuf);
299201941Smarcel	return (error);
300201941Smarcel}
301