+fsw_status_t EFIAPI fsw_efi_read_block(struct fsw_volume *vol, fsw_u64 phys_bno, void *buffer) {
+ int i, ReadCache = -1;
+ FSW_VOLUME_DATA *Volume = (FSW_VOLUME_DATA *)vol->host_data;
+ EFI_STATUS Status = EFI_SUCCESS;
+ BOOLEAN ReadOneBlock = FALSE;
+ UINT64 StartRead = (UINT64) phys_bno * (UINT64) vol->phys_blocksize;
+
+ if (buffer == NULL)
+ return (fsw_status_t) EFI_BAD_BUFFER_SIZE;
+
+ // Initialize static data structures, if necessary....
+ if (LastRead < 0) {
+ fsw_efi_clear_cache();
+ } // if
+
+ // Look for a cache hit on the current query....
+ i = 0;
+ do {
+ if ((Caches[i].Volume == Volume) &&
+ (Caches[i].CacheValid == TRUE) &&
+ (StartRead >= Caches[i].CacheStart) &&
+ ((StartRead + vol->phys_blocksize) <= (Caches[i].CacheStart + CACHE_SIZE))) {
+ ReadCache = i;
+ }
+ i++;
+ } while ((i < NUM_CACHES) && (ReadCache < 0));
+
+ // No cache hit found; load new cache and pass it on....
+ if (ReadCache < 0) {
+ if (LastRead == -1)
+ LastRead = 1;
+ ReadCache = 1 - LastRead; // NOTE: If NUM_CACHES > 2, this must become more complex
+ Caches[ReadCache].CacheValid = FALSE;
+ if (Caches[ReadCache].Cache == NULL)
+ Caches[ReadCache].Cache = AllocatePool(CACHE_SIZE);
+ if (Caches[ReadCache].Cache != NULL) {
+ // TODO: Below call hangs on my 32-bit Mac Mini when compiled with GNU-EFI.
+ // The same binary is fine under VirtualBox, and the same call is fine when
+ // compiled with Tianocore. Further clue: Omitting "Status =" avoids the
+ // hang but produces a failure to mount the filesystem, even when the same
+ // change is made to later similar call. Calling Volume->DiskIo->ReadDisk()
+ // directly (without refit_call5_wrapper()) changes nothing. Placing Print()
+ // statements at the start and end of the function, and before and after the
+ // ReadDisk() call, suggests that when it fails, the program is executing
+ // code starting mid-function, so there seems to be something messed up in
+ // the way the function is being called. FIGURE THIS OUT!
+ Status = refit_call5_wrapper(Volume->DiskIo->ReadDisk, Volume->DiskIo, Volume->MediaId,
+ StartRead, (UINTN) CACHE_SIZE, (VOID*) Caches[ReadCache].Cache);
+ if (!EFI_ERROR(Status)) {
+ Caches[ReadCache].CacheStart = StartRead;
+ Caches[ReadCache].CacheValid = TRUE;
+ Caches[ReadCache].Volume = Volume;
+ LastRead = ReadCache;
+ } else {
+ ReadOneBlock = TRUE;
+ }
+ } else {
+ ReadOneBlock = TRUE;
+ } // if cache memory allocated
+ } // if (ReadCache < 0)
+
+ if (Caches[ReadCache].Cache != NULL && Caches[ReadCache].CacheValid == TRUE && vol->phys_blocksize > 0) {
+ CopyMem(buffer, &Caches[ReadCache].Cache[StartRead - Caches[ReadCache].CacheStart], vol->phys_blocksize);
+ } else {
+ ReadOneBlock = TRUE;
+ }
+
+ if (ReadOneBlock) { // Something's failed, so try a simple disk read of one block....
+ Status = refit_call5_wrapper(Volume->DiskIo->ReadDisk, Volume->DiskIo, Volume->MediaId,
+ phys_bno * vol->phys_blocksize,
+ (UINTN) vol->phys_blocksize,
+ (VOID*) buffer);
+ }
+ Volume->LastIOStatus = Status;
+
+ return Status;
+} // fsw_status_t *fsw_efi_read_block()