5
5
* with fallback to direct PCI configuration space access for specific chipsets.
6
6
*/
7
7
8
+ #include <stdbool.h>
8
9
#include <efi.h>
9
10
#include "csmwrap.h"
10
11
#include "edk2/LegacyRegion2.h"
@@ -298,6 +299,32 @@ EFI_STATUS print_legacy_region_info(EFI_LEGACY_REGION2_PROTOCOL *legacy_region)
298
299
return EFI_SUCCESS ;
299
300
}
300
301
302
+ static bool test_bios_region_rw (void ) {
303
+ uint32_t * bios_region = (uint32_t * )BIOSROM_START ;
304
+ uint32_t * bios_region_end = (uint32_t * )BIOSROM_END ;
305
+ uint32_t * ptr = bios_region ;
306
+
307
+ while (ptr < bios_region_end ) {
308
+ uint32_t val ;
309
+
310
+ clflush (ptr );
311
+ val = readl (ptr );
312
+
313
+ writel (ptr , ~val );
314
+ clflush (ptr );
315
+
316
+ if (readl (ptr ) != ~val ) {
317
+ printf ("Unable to write to BIOS region\n" );
318
+ return false;
319
+ }
320
+
321
+ writel (ptr , val );
322
+ ptr ++ ;
323
+ }
324
+
325
+ return true;
326
+ }
327
+
301
328
/**
302
329
* Main function to unlock the BIOS region
303
330
* Tries to use the UEFI protocol first, then falls back to chipset-specific methods
@@ -309,6 +336,11 @@ int unlock_bios_region(void)
309
336
EFI_LEGACY_REGION2_PROTOCOL * legacy_region = NULL ;
310
337
EFI_STATUS status ;
311
338
339
+ // No need to do anything if the region is already unlocked and working.
340
+ if (test_bios_region_rw ()) {
341
+ return 0 ;
342
+ }
343
+
312
344
/* First, try to use the Legacy Region 2 Protocol */
313
345
status = gBS -> LocateProtocol (
314
346
& gEfiLegacyRegion2ProtocolGuid ,
@@ -322,7 +354,7 @@ int unlock_bios_region(void)
322
354
323
355
/* Try to unlock using the protocol */
324
356
status = unlock_legacy_region_protocol ();
325
- if (!EFI_ERROR (status )) {
357
+ if (!EFI_ERROR (status ) && test_bios_region_rw () ) {
326
358
return 0 ; /* Success */
327
359
}
328
360
@@ -367,5 +399,5 @@ int unlock_bios_region(void)
367
399
break ;
368
400
}
369
401
370
- return status == 0 ? 0 : -1 ;
402
+ return ( status == 0 && test_bios_region_rw ()) ? 0 : -1 ;
371
403
}
0 commit comments