Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1NSRuH-0008Sr-NQ for mharc-grub-devel@gnu.org; Wed, 06 Jan 2010 04:10:21 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NSRuF-0008Qe-Hj for grub-devel@gnu.org; Wed, 06 Jan 2010 04:10:19 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NSRuB-0008Lu-Pp for grub-devel@gnu.org; Wed, 06 Jan 2010 04:10:19 -0500 Received: from [199.232.76.173] (port=37110 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NSRuB-0008Lm-M1 for grub-devel@gnu.org; Wed, 06 Jan 2010 04:10:15 -0500 Received: from mga01.intel.com ([192.55.52.88]:17920) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NSRuB-0003pt-8Q for grub-devel@gnu.org; Wed, 06 Jan 2010 04:10:15 -0500 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP; 06 Jan 2010 01:09:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.49,226,1262592000"; d="scan'208";a="761748803" Received: from debian.sh.intel.com (HELO localhost.localdomain) ([10.239.13.65]) by fmsmga001.fm.intel.com with ESMTP; 06 Jan 2010 01:10:02 -0800 From: Zhu Yi To: rmh@aybabtu.com Date: Wed, 6 Jan 2010 17:10:03 +0800 Message-Id: <1262769004-21960-1-git-send-email-yi.zhu@intel.com> X-Mailer: git-send-email 1.6.3.3 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: grub-devel@gnu.org, Zhu Yi , Zhu Yi Subject: [PATCH 1/2] Backup old boot sectors before installation X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 06 Jan 2010 09:10:19 -0000 From: Zhu Yi Make grub-setup backup the old boot sectors into a file (bootsectors.bak) before overwriting it. The backup image starts from the MBR to the end of core.img position (including embed regions). This makes it possible for a user to later restore it with dd or other methods. Signed-off-by: Zhu Yi --- util/i386/pc/grub-setup.c | 33 +++++++++++++++++++++++++++++++++ 1 files changed, 33 insertions(+), 0 deletions(-) diff --git a/util/i386/pc/grub-setup.c b/util/i386/pc/grub-setup.c index c536be0..b045e23 100644 --- a/util/i386/pc/grub-setup.c +++ b/util/i386/pc/grub-setup.c @@ -53,6 +53,7 @@ static const grub_gpt_part_type_t grub_gpt_partition_type_bios_boot = GRUB_GPT_P #define DEFAULT_BOOT_FILE "boot.img" #define DEFAULT_CORE_FILE "core.img" +#define DEFAULT_BACKUP_FILE "bootsectors.bak" /* This is the blocklist used in the diskboot image. */ struct boot_blocklist @@ -396,6 +397,33 @@ setup (const char *dir, block->len = 0; block->segment = 0; + int grub_disk_backup(grub_disk_t disk, grub_size_t size, const char *path) + { + char *tmp_buf; + + grub_util_info ("opening the backup file `%s'", path); + fp = fopen (path, "wb"); + if (! fp) + return -1; + + tmp_buf = xmalloc (size); + if (grub_disk_read (disk, 0, 0, size, tmp_buf) != GRUB_ERR_NONE) { + fclose (fp); + return -1; + } + + grub_util_write_image (tmp_buf, size, fp); + + fclose (fp); + return 0; + } + + /* Backup old boot sectors */ + if (grub_disk_backup (dest_dev->disk, + GRUB_DISK_SECTOR_SIZE * embed_region.start + core_size, + grub_util_get_path (dir, DEFAULT_BACKUP_FILE))) + grub_util_error ("failed to backup previous boot sectors"); + /* Write the core image onto the disk. */ if (grub_disk_write (dest_dev->disk, embed_region.start, 0, core_size, core_img)) grub_util_error ("%s", grub_errmsg); @@ -548,6 +576,11 @@ unable_to_embed: grub_util_write_image (core_img, GRUB_DISK_SECTOR_SIZE * 2, fp); fclose (fp); + /* Backup MBR only */ + if (grub_disk_backup (dest_dev->disk, GRUB_DISK_SECTOR_SIZE, + grub_util_get_path (dir, DEFAULT_BACKUP_FILE))) + grub_util_error ("failed to backup previous boot sectors"); + /* Write the boot image onto the disk. */ if (grub_disk_write (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, boot_img)) grub_util_error ("%s", grub_errmsg); -- 1.5.3.6