Util-Linux package development
 help / color / mirror / Atom feed
* [PATCH V2] dos: copy previous bootsector for dos partition table
@ 2015-04-04 16:05 Jean-Loup 'clippix' Bogalho
  2015-04-07  9:45 ` Karel Zak
  0 siblings, 1 reply; 3+ messages in thread
From: Jean-Loup 'clippix' Bogalho @ 2015-04-04 16:05 UTC (permalink / raw)
  To: util-linux; +Cc: gabriel

In order to create a dos partition scheme, libfdisk erase the first disk sector
and then add the needed values (partitions, magic numbers).
In the case the disk already contains code in the bootsection, it will be
erased.  This was not the case in v2.25 of util-linux.
Since fdisk can be used to repair messed partition tables, maybe we don't want
to have to rewrite the boot section.
If you want to keep the precedent behavior, here's a patch that re-copy the old
bootsection after it has been wiped. There is other ways to do it, but this one
doesn't interfere with the rest of the code.

Signed-off-by: Jean-Loup 'clippix' Bogalho <clippix@lse.epita.fr>
---
 libfdisk/src/dos.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/libfdisk/src/dos.c b/libfdisk/src/dos.c
index dce9829..998080a 100644
--- a/libfdisk/src/dos.c
+++ b/libfdisk/src/dos.c
@@ -18,6 +18,7 @@
 
 #define MAXIMUM_PARTS	60
 #define ACTIVE_FLAG     0x80
+#define BOOTSECTOR_SIZE	446
 
 /**
  * SECTION: dos
@@ -659,9 +660,17 @@ static int dos_create_disklabel(struct fdisk_context *cxt)
 	if (!has_id)
 		random_get_bytes(&id, sizeof(id));
 
-	rc = fdisk_init_firstsector_buffer(cxt);
-	if (rc)
-		return rc;
+	if (cxt->firstsector && cxt->firstsector_bufsz == cxt->sector_size) {
+		unsigned char bootsector[BOOTSECTOR_SIZE];
+		memcpy(bootsector, cxt->firstsector, BOOTSECTOR_SIZE);
+		rc = fdisk_init_firstsector_buffer(cxt);
+		memcpy(cxt->firstsector, bootsector, BOOTSECTOR_SIZE);
+	}
+	else {
+		rc = fdisk_init_firstsector_buffer(cxt);
+		if (rc)
+			return rc;
+	}
 	dos_init(cxt);
 
 	l = self_label(cxt);

-- 
Jean-Loup 'clippix' Bogalho
LSE 2016

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-04-13 11:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-04-04 16:05 [PATCH V2] dos: copy previous bootsector for dos partition table Jean-Loup 'clippix' Bogalho
2015-04-07  9:45 ` Karel Zak
2015-04-13 11:20   ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox