REF:https://bugzilla.tianocore.org/show_bug.cgi?id=1134
Originally, the block size of created Ram disks is hard-coded to 512
bytes. However, if the total size of the Ram disk is not a multiple of 512
bytes, there will be potential memory access issues when dealing with the
last block of the Ram disk.
This commit will adjust the block size of the Ram disks to ensure that the
total size is a multiple of the block size.
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Star Zeng <star.zeng@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
/** @file\r
Produce EFI_BLOCK_IO_PROTOCOL on a RAM disk device.\r
\r
/** @file\r
Produce EFI_BLOCK_IO_PROTOCOL on a RAM disk device.\r
\r
- Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
+ Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
which accompanies this distribution. The full text of the license may be found at\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
which accompanies this distribution. The full text of the license may be found at\r
EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
EFI_BLOCK_IO2_PROTOCOL *BlockIo2;\r
EFI_BLOCK_IO_MEDIA *Media;\r
EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
EFI_BLOCK_IO2_PROTOCOL *BlockIo2;\r
EFI_BLOCK_IO_MEDIA *Media;\r
\r
BlockIo = &PrivateData->BlockIo;\r
BlockIo2 = &PrivateData->BlockIo2;\r
\r
BlockIo = &PrivateData->BlockIo;\r
BlockIo2 = &PrivateData->BlockIo2;\r
Media->LogicalPartition = FALSE;\r
Media->ReadOnly = FALSE;\r
Media->WriteCaching = FALSE;\r
Media->LogicalPartition = FALSE;\r
Media->ReadOnly = FALSE;\r
Media->WriteCaching = FALSE;\r
- Media->BlockSize = RAM_DISK_BLOCK_SIZE;\r
- Media->LastBlock = DivU64x32 (\r
- PrivateData->Size + RAM_DISK_BLOCK_SIZE - 1,\r
- RAM_DISK_BLOCK_SIZE\r
- ) - 1;\r
+\r
+ for (Media->BlockSize = RAM_DISK_DEFAULT_BLOCK_SIZE;\r
+ Media->BlockSize >= 1;\r
+ Media->BlockSize = Media->BlockSize >> 1) {\r
+ Media->LastBlock = DivU64x32Remainder (PrivateData->Size, Media->BlockSize, &Remainder) - 1;\r
+ if (Remainder == 0) {\r
+ break;\r
+ }\r
+ }\r
+ ASSERT (Media->BlockSize != 0);\r
+\r
+ return;\r
/** @file\r
The header file of RamDiskDxe driver.\r
\r
/** @file\r
The header file of RamDiskDxe driver.\r
\r
- Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
+ Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
which accompanies this distribution. The full text of the license may be found at\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
which accompanies this distribution. The full text of the license may be found at\r
-// Block size for RAM disk\r
+// Default block size for RAM disk\r
-#define RAM_DISK_BLOCK_SIZE 512\r
+#define RAM_DISK_DEFAULT_BLOCK_SIZE 512\r
\r
//\r
// Iterate through the double linked list. NOT delete safe\r
\r
//\r
// Iterate through the double linked list. NOT delete safe\r
/** @file\r
The realization of EFI_RAM_DISK_PROTOCOL.\r
\r
/** @file\r
The realization of EFI_RAM_DISK_PROTOCOL.\r
\r
- Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
+ Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>\r
(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
//\r
// Add check to prevent data read across the memory boundary\r
//\r
//\r
// Add check to prevent data read across the memory boundary\r
//\r
- if (RamDiskBase + RamDiskSize > ((UINTN) -1) - RAM_DISK_BLOCK_SIZE + 1) {\r
+ if ((RamDiskSize > MAX_UINTN) ||\r
+ (RamDiskBase > MAX_UINTN - RamDiskSize + 1)) {\r
return EFI_INVALID_PARAMETER;\r
}\r
\r
return EFI_INVALID_PARAMETER;\r
}\r
\r