[parted-devel] [PATCH 1/2] libparted: Stop adding boot code in MBR when updating msdos table

Mike Fleetwood mike.fleetwood at googlemail.com
Fri Oct 24 14:03:02 BST 2025


fdisk from util-linux doesn't write boot code into the msdos MBR when it
updates the partition table.  Neither does libparted when updating the
protective partition in the MBR as part of a GPT.  Writing boot code is
the responsibility of boot loader installation, not a partitioning tool.

New behaviour, matching what fdisk does:
1. If there's no recognisable msdos partition table then a new MBR will
   be written including zeroed boot code.
2. When updating an msdos partition table existing boot code in the MBR
   will be maintained.

Original report:
Unwanted install of loader in MBR
https://gitlab.gnome.org/GNOME/gparted/-/issues/205

Signed-off-by: Mike Fleetwood <mike.fleetwood at googlemail.com>
---
 NEWS                   |  7 +++-
 libparted/labels/dos.c | 22 -----------
 libparted/mbr.s        | 85 ------------------------------------------
 3 files changed, 6 insertions(+), 108 deletions(-)
 delete mode 100644 libparted/mbr.s

diff --git a/NEWS b/NEWS
index 7cfca99..27b12d9 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,12 @@ GNU parted NEWS                                    -*- outline -*-
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** New Features
+
+** Bug Fixes
+
+  Stop adding boot code into the MBR if it's zero when updating an
+  existing msdos partition table.
 
 * Noteworthy changes in release 3.6 (2023-04-10) [stable]
 
@@ -19,7 +25,6 @@ GNU parted NEWS                                    -*- outline -*-
 
   Add display of GPT disk and partition UUIDs in JSON output
 
-
 ** Bug Fixes
 
   Fix use of enums in flag limits by switching to using #define
diff --git a/libparted/labels/dos.c b/libparted/labels/dos.c
index e6a0105..443209e 100644
--- a/libparted/labels/dos.c
+++ b/libparted/labels/dos.c
@@ -35,23 +35,6 @@
 #include "misc.h"
 #include "pt-tools.h"
 
-/* this MBR boot code is loaded into 0000:7c00 by the BIOS.  See mbr.s for
- * the source, and how to build it
- */
-
-static const char MBR_BOOT_CODE[] = {
-	0xfa, 0xb8, 0x00, 0x10, 0x8e, 0xd0, 0xbc, 0x00,
-	0xb0, 0xb8, 0x00, 0x00, 0x8e, 0xd8, 0x8e, 0xc0,
-	0xfb, 0xbe, 0x00, 0x7c, 0xbf, 0x00, 0x06, 0xb9,
-	0x00, 0x02, 0xf3, 0xa4, 0xea, 0x21, 0x06, 0x00,
-	0x00, 0xbe, 0xbe, 0x07, 0x38, 0x04, 0x75, 0x0b,
-	0x83, 0xc6, 0x10, 0x81, 0xfe, 0xfe, 0x07, 0x75,
-	0xf3, 0xeb, 0x16, 0xb4, 0x02, 0xb0, 0x01, 0xbb,
-	0x00, 0x7c, 0xb2, 0x80, 0x8a, 0x74, 0x01, 0x8b,
-	0x4c, 0x02, 0xcd, 0x13, 0xea, 0x00, 0x7c, 0x00,
-	0x00, 0xeb, 0xfe
-};
-
 #define MSDOS_MAGIC		0xAA55
 #define PARTITION_MAGIC_MAGIC	0xf6f6
 
@@ -1443,11 +1426,6 @@ msdos_write (const PedDisk* disk)
 		return 0;
 	DosRawTable *table = (DosRawTable *) s0;
 
-	if (!table->boot_code[0]) {
-		memset (table, 0, 512);
-		memcpy (table->boot_code, MBR_BOOT_CODE, sizeof (MBR_BOOT_CODE));
-	}
-
 	/* If there is no unique identifier, generate a random one */
 	if (!table->mbr_signature)
 		table->mbr_signature = generate_random_uint32 ();
diff --git a/libparted/mbr.s b/libparted/mbr.s
deleted file mode 100644
index 8ac2240..0000000
--- a/libparted/mbr.s
+++ /dev/null
@@ -1,85 +0,0 @@
-;   libparted - a library for manipulating disk partitions
-;   Copyright (C) 1999-2000, 2007, 2009-2014, 2019-2023 Free Software
-;   Foundation, Inc.
-;
-;   This program is free software; you can redistribute it and/or modify
-;   it under the terms of the GNU General Public License as published by
-;   the Free Software Foundation; either version 3 of the License, or
-;   (at your option) any later version.
-;
-;   This program is distributed in the hope that it will be useful,
-;   but WITHOUT ANY WARRANTY; without even the implied warranty of
-;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-;   GNU General Public License for more details.
-;
-;   You should have received a copy of the GNU General Public License
-;   along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-; NOTE: I build this with:
-;	$ as86 -b /dev/stdout mbr.s | hexdump -e '8/1 "0x%02x, " "\n"'
-;
-; The build isn't done automagically by make, because as86 may not be on many
-; machines (particularly non-x86).  Also, it seems rather difficult to get
-; as86 to build object files that can be linked, especially as it's 16 bit
-; code...
-
-USE16
-
-; This code, plus the partition table is loaded into 0000:7C00 by the BIOS
-
-.text
-
-; set top of stack to 1000:B000
-
-	cli
-
-	mov	ax, #0x1000
-	mov	ss, ax
-	mov	sp, #0xB000
-
-	mov	ax, #0x0000
-	mov	ds, ax
-	mov	es, ax
-
-	sti
-
-; Copy what the BIOS loaded (i.e. the MBR + head of partition table) from
-; 0000:7c00 to 0000:0600
-
-	mov	si, #0x7c00
-	mov	di, #0x0600
-	mov	cx, #0x200
-	rep
-	movsb
-
-; Jump to the copy of the MBR
-
-	jmp	0x0000:find_boot_partition + 0x0600
-
-find_boot_partition:
-	mov	si, #0x07BE
-
-check_next_bootable:
-	cmp	[si], al
-	jnz	found_bootable
-	add	si, #0x0010
-	cmp	si, #0x07FE
-	jnz	check_next_bootable
-	jmp	error
-
-found_bootable:
-
-; Load in the boot sector at 0000:7c00
-
-	mov	ah, #2			; BIOS command (read)
-	mov	al, #1			; count
-	mov	bx, #0x7c00		; destination pointer
-	mov	dl, #0x80		; drive
-	mov	dh, byte ptr [si + 1]	; head
-	mov	cx, word ptr [si + 2]	; sector / cylinder
-	int	#0x13			; BIOS read interrupt
-
-	jmp	0x0000:0x7c00		; hand control to boot sector
-
-error:
-	jmp	error
-- 
2.43.7




More information about the parted-devel mailing list