From 3c33e37d64e44addc9a818bd556f5919ed2e9002 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Fri, 6 Sep 2024 07:51:13 -0700 Subject: [PATCH] Avoid detecting FAT VBR as an MBR The 1980s FAT filesystem has a VBR in the first sector, which looks almost exactly like an MBR with zero partitions. To avoid detecting these as MBRs, look for some extra attributes that indicate that the structure is a VBR and avoid matching it as a GPT/MBR in that case. We can add an inspector for this as a separate thing, but at the moment we don't have that immediate need. Closes-Bug: #2079850 Change-Id: Ibad87743b5a3b6469bd708d4caafe7911b045855 --- oslo_utils/imageutils/format_inspector.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/oslo_utils/imageutils/format_inspector.py b/oslo_utils/imageutils/format_inspector.py index 02d362f0..7463c670 100644 --- a/oslo_utils/imageutils/format_inspector.py +++ b/oslo_utils/imageutils/format_inspector.py @@ -1214,6 +1214,7 @@ class GPTInspector(FileInspector): NAME = 'gpt' MBR_SIGNATURE = 0xAA55 MBR_PTE_START = 446 + MEDIA_TYPE_FDISK = 0xF8 def _initialize(self): self.new_region('mbr', CaptureRegion(0, 512)) @@ -1224,12 +1225,28 @@ class GPTInspector(FileInspector): # self.new_region('gpt_backup', EndCaptureRegion(512)) self.add_safety_check(SafetyCheck('mbr', self.check_mbr_partitions)) + def _check_for_fat(self): + # A FAT filesystem looks like an MBR, but actually starts with a VBR, + # which has the same signature as an MBR, but with more specifics in + # the BPB (BIOS Parameter Block). + boot_sector = self.region('mbr').data + # num_fats is almost always 2 (never more or less) for any filesystem + # not super tiny (think 1980s ramdisk) + num_fats = boot_sector[0x10] + # Media descriptor will basically always be "a fixed disk" for any of + # our purposes, not a floppy disk + media_desc = boot_sector[0x15] + return (num_fats == 2 and media_desc == self.MEDIA_TYPE_FDISK) + @property def format_match(self): if not self.region('mbr').complete: return False + # Check to see if this looks like a VBR from a FAT filesystem so we + # can exclude it + is_fat = self._check_for_fat() mbr_sig, = struct.unpack('