Module Name:    src
Committed By:   mlelstv
Date:           Sun Apr 24 06:49:38 UTC 2022

Modified Files:
        src/sys/stand/efiboot: conf.c efiblock.c efiblock.h

Log Message:
Use physical sector size as unit for disk addresses.
Provide new ioctl to libsa to query for sector size.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/stand/efiboot/conf.c
cvs rdiff -u -r1.18 -r1.19 src/sys/stand/efiboot/efiblock.c
cvs rdiff -u -r1.6 -r1.7 src/sys/stand/efiboot/efiblock.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/stand/efiboot/conf.c
diff -u src/sys/stand/efiboot/conf.c:1.5 src/sys/stand/efiboot/conf.c:1.6
--- src/sys/stand/efiboot/conf.c:1.5	Sun Oct 11 14:03:33 2020
+++ src/sys/stand/efiboot/conf.c	Sun Apr 24 06:49:38 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: conf.c,v 1.5 2020/10/11 14:03:33 jmcneill Exp $ */
+/* $NetBSD: conf.c,v 1.6 2022/04/24 06:49:38 mlelstv Exp $ */
 
 /*-
  * Copyright (c) 2018 Jared McNeill <[email protected]>
@@ -41,7 +41,7 @@
 
 struct devsw devsw[] = {
 	{ "efifile", efi_file_strategy, efi_file_open, efi_file_close, noioctl },
-	{ "efiblock", efi_block_strategy, efi_block_open, efi_block_close, noioctl },
+	{ "efiblock", efi_block_strategy, efi_block_open, efi_block_close, efi_block_ioctl },
 	{ "net", net_strategy, net_open, net_close, noioctl },
 };
 int ndevs = __arraycount(devsw);

Index: src/sys/stand/efiboot/efiblock.c
diff -u src/sys/stand/efiboot/efiblock.c:1.18 src/sys/stand/efiboot/efiblock.c:1.19
--- src/sys/stand/efiboot/efiblock.c:1.18	Sat Oct 30 11:18:51 2021
+++ src/sys/stand/efiboot/efiblock.c	Sun Apr 24 06:49:38 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: efiblock.c,v 1.18 2021/10/30 11:18:51 jmcneill Exp $ */
+/* $NetBSD: efiblock.c,v 1.19 2022/04/24 06:49:38 mlelstv Exp $ */
 
 /*-
  * Copyright (c) 2016 Kimihiro Nonaka <[email protected]>
@@ -129,20 +129,21 @@ efi_block_do_read_blockio(struct efi_blo
 	EFI_STATUS status;
 	EFI_LBA lba_start, lba_end;
 	UINT64 blkbuf_offset;
-	UINT64 blkbuf_size;
+	UINT64 blkbuf_size, alloc_size;
 
 	lba_start = off / bdev->bio->Media->BlockSize;
-	lba_end = (off + bufsize + bdev->bio->Media->BlockSize - 1) /
-	    bdev->bio->Media->BlockSize;
+	lba_end = (off + bufsize - 1) / bdev->bio->Media->BlockSize;
 	blkbuf_offset = off % bdev->bio->Media->BlockSize;
-	blkbuf_size = (lba_end - lba_start) * bdev->bio->Media->BlockSize;
+	blkbuf_size = (lba_end - lba_start + 1) * bdev->bio->Media->BlockSize;
+
+	alloc_size = blkbuf_size;
 	if (bdev->bio->Media->IoAlign > 1) {
-		blkbuf_size = (blkbuf_size + bdev->bio->Media->IoAlign - 1) /
+		alloc_size = (blkbuf_size + bdev->bio->Media->IoAlign - 1) /
 		    bdev->bio->Media->IoAlign *
 		    bdev->bio->Media->IoAlign;
 	}
 
-	blkbuf = AllocatePool(blkbuf_size);
+	blkbuf = AllocatePool(alloc_size);
 	if (blkbuf == NULL) {
 		return EFI_OUT_OF_RESOURCES;
 	}
@@ -285,18 +286,16 @@ efi_block_find_partitions_disklabel(stru
     struct mbr_sector *mbr, uint32_t start, uint32_t size)
 {
 	struct efi_block_part *bpart;
-	char buf[DEV_BSIZE];
+	char buf[DEV_BSIZE]; /* XXX, arbitrary size >= struct disklabel */
 	struct disklabel d;
 	struct partition *p;
 	EFI_STATUS status;
 	int n;
 
 	status = efi_block_read(bdev,
-	    ((EFI_LBA)start + LABELSECTOR) * DEV_BSIZE, buf, sizeof(buf));
-	if (EFI_ERROR(status) || getdisklabel(buf, &d) != NULL) {
-		FreePool(buf);
+	    ((EFI_LBA)start + LABELSECTOR) * bdev->bio->Media->BlockSize, buf, sizeof(buf));
+	if (EFI_ERROR(status) || getdisklabel(buf, &d) != NULL)
 		return EIO;
-	}
 
 	if (le32toh(d.d_magic) != DISKMAGIC || le32toh(d.d_magic2) != DISKMAGIC)
 		return EINVAL;
@@ -419,7 +418,7 @@ efi_block_find_partitions_gpt(struct efi
 	void *buf;
 	UINTN sz;
 
-	status = efi_block_read(bdev, GPT_HDR_BLKNO * DEV_BSIZE, &hdr,
+	status = efi_block_read(bdev, (EFI_LBA)GPT_HDR_BLKNO * bdev->bio->Media->BlockSize, &hdr,
 	    sizeof(hdr));
 	if (EFI_ERROR(status)) {
 		return EIO;
@@ -436,7 +435,7 @@ efi_block_find_partitions_gpt(struct efi
 		return ENOMEM;
 
 	status = efi_block_read(bdev,
-	    le64toh(hdr.hdr_lba_table) * DEV_BSIZE, buf, sz);
+	    le64toh(hdr.hdr_lba_table) * bdev->bio->Media->BlockSize, buf, sz);
 	if (EFI_ERROR(status)) {
 		FreePool(buf);
 		return EIO;
@@ -682,6 +681,7 @@ int
 efi_block_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
 {
 	struct efi_block_part *bpart = devdata;
+	struct efi_block_dev *bdev = bpart->bdev;
 	EFI_STATUS status;
 	UINT64 off;
 
@@ -692,13 +692,13 @@ efi_block_strategy(void *devdata, int rw
 
 	switch (bpart->type) {
 	case EFI_BLOCK_PART_DISKLABEL:
-		off = (dblk + bpart->disklabel.part.p_offset) * DEV_BSIZE;
+		off = ((EFI_LBA)dblk + bpart->disklabel.part.p_offset) * bdev->bio->Media->BlockSize;
 		break;
 	case EFI_BLOCK_PART_GPT:
-		off = (dblk + le64toh(bpart->gpt.ent.ent_lba_start)) * DEV_BSIZE;
+		off = ((EFI_LBA)dblk + le64toh(bpart->gpt.ent.ent_lba_start)) * bdev->bio->Media->BlockSize;
 		break;
 	case EFI_BLOCK_PART_CD9660:
-		off = dblk * ISO_DEFAULT_BLOCK_SIZE;
+		off = (EFI_LBA)dblk * ISO_DEFAULT_BLOCK_SIZE;
 		break;
 	default:
 		return EINVAL;
@@ -718,3 +718,22 @@ efi_block_set_readahead(bool onoff)
 {
 	efi_ra_enable = onoff;
 }
+
+int
+efi_block_ioctl(struct open_file *f, u_long cmd, void *data)
+{
+	struct efi_block_part *bpart = f->f_devdata;
+	struct efi_block_dev *bdev = bpart->bdev;
+	int error = 0;
+
+	switch (cmd) {
+	case SAIOSECSIZE:
+		*(u_int *)data = bdev->bio->Media->BlockSize;
+		break;
+	default:
+		error = ENOTTY;
+		break;
+	}
+
+	return error;
+}

Index: src/sys/stand/efiboot/efiblock.h
diff -u src/sys/stand/efiboot/efiblock.h:1.6 src/sys/stand/efiboot/efiblock.h:1.7
--- src/sys/stand/efiboot/efiblock.h:1.6	Mon Jun 21 21:18:47 2021
+++ src/sys/stand/efiboot/efiblock.h	Sun Apr 24 06:49:38 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: efiblock.h,v 1.6 2021/06/21 21:18:47 jmcneill Exp $ */
+/* $NetBSD: efiblock.h,v 1.7 2022/04/24 06:49:38 mlelstv Exp $ */
 
 /*-
  * Copyright (c) 2018 Jared McNeill <[email protected]>
@@ -79,6 +79,7 @@ struct efi_block_part *efi_block_boot_pa
 
 int efi_block_open(struct open_file *, ...);
 int efi_block_close(struct open_file *);
+int efi_block_ioctl(struct open_file *, u_long, void *);
 int efi_block_strategy(void *, int, daddr_t, size_t, void *, size_t *);
 
 void efi_block_set_readahead(bool);

Reply via email to