Adding upstream version 6.03~pre2+dfsg.
[syslinux-debian/hramrach.git] / efi / diskio.c
blobd6a160e1bdba67a2243ce94e47c02a09e9fe0977
1 /*
2 * Copyright 2011-2014 Intel Corporation - All Rights Reserved
3 */
5 #include <fs.h>
6 #include <ilog2.h>
7 #include <disk.h>
8 #include <dprintf.h>
9 #include "efi.h"
11 static inline EFI_STATUS read_blocks(EFI_BLOCK_IO *bio, uint32_t id,
12 sector_t lba, UINTN bytes, void *buf)
14 return uefi_call_wrapper(bio->ReadBlocks, 5, bio, id, lba, bytes, buf);
17 static inline EFI_STATUS write_blocks(EFI_BLOCK_IO *bio, uint32_t id,
18 sector_t lba, UINTN bytes, void *buf)
20 return uefi_call_wrapper(bio->WriteBlocks, 5, bio, id, lba, bytes, buf);
23 static int efi_rdwr_sectors(struct disk *disk, void *buf,
24 sector_t lba, size_t count, bool is_write)
26 struct efi_disk_private *priv = (struct efi_disk_private *)disk->private;
27 EFI_BLOCK_IO *bio = priv->bio;
28 EFI_STATUS status;
29 UINTN bytes = count * disk->sector_size;
31 if (is_write)
32 status = write_blocks(bio, disk->disk_number, lba, bytes, buf);
33 else
34 status = read_blocks(bio, disk->disk_number, lba, bytes, buf);
36 if (status != EFI_SUCCESS)
37 Print(L"Failed to %s blocks: 0x%x\n",
38 is_write ? L"write" : L"read",
39 status);
41 return count << disk->sector_shift;
44 struct disk *efi_disk_init(void *private)
46 static struct disk disk;
47 struct efi_disk_private *priv = (struct efi_disk_private *)private;
48 EFI_HANDLE handle = priv->dev_handle;
49 EFI_BLOCK_IO *bio;
50 EFI_DISK_IO *dio;
51 EFI_STATUS status;
53 status = uefi_call_wrapper(BS->HandleProtocol, 3, handle,
54 &DiskIoProtocol, (void **)&dio);
55 if (status != EFI_SUCCESS)
56 return NULL;
58 status = uefi_call_wrapper(BS->HandleProtocol, 3, handle,
59 &BlockIoProtocol, (void **)&bio);
60 if (status != EFI_SUCCESS)
61 return NULL;
64 * XXX Do we need to map this to a BIOS disk number?
66 disk.disk_number = bio->Media->MediaId;
68 disk.sector_size = bio->Media->BlockSize;
69 disk.rdwr_sectors = efi_rdwr_sectors;
70 disk.sector_shift = ilog2(disk.sector_size);
72 dprintf("sector_size=%d, disk_number=%d\n", disk.sector_size,
73 disk.disk_number);
75 priv->bio = bio;
76 priv->dio = dio;
77 disk.private = private;
78 #if 0
80 disk.part_start = part_start;
81 disk.secpercyl = disk.h * disk.s;
84 disk.maxtransfer = MaxTransfer;
86 dprintf("disk %02x cdrom %d type %d sector %u/%u offset %llu limit %u\n",
87 media_id, cdrom, ebios, sector_size, disk.sector_shift,
88 part_start, disk.maxtransfer);
89 #endif
91 return &disk;