Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1M35d9-0000Ik-DF for mharc-grub-devel@gnu.org; Sun, 10 May 2009 05:47:35 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1M35d6-0000Hh-V4 for grub-devel@gnu.org; Sun, 10 May 2009 05:47:33 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1M35d2-0000GT-Px for grub-devel@gnu.org; Sun, 10 May 2009 05:47:32 -0400 Received: from [199.232.76.173] (port=53414 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1M35d2-0000GN-Da for grub-devel@gnu.org; Sun, 10 May 2009 05:47:28 -0400 Received: from aybabtu.com ([69.60.117.155]:59189) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1M35d2-000078-0B for grub-devel@gnu.org; Sun, 10 May 2009 05:47:28 -0400 Received: from [192.168.10.10] (helo=thorin) by aybabtu.com with esmtp (Exim 4.69) (envelope-from ) id 1M35Oz-0004cN-5J for grub-devel@gnu.org; Sun, 10 May 2009 11:32:57 +0200 Received: from rmh by thorin with local (Exim 4.69) (envelope-from ) id 1M35cx-000847-7n for grub-devel@gnu.org; Sun, 10 May 2009 11:47:23 +0200 Date: Sun, 10 May 2009 11:47:23 +0200 From: Robert Millan To: grub-devel@gnu.org Message-ID: <20090510094723.GA30969@thorin> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="a8Wt8u1KmwUX3Y2C" Content-Disposition: inline Organization: free as in freedom X-Message-Flag: Worried about Outlook viruses? Switch to Thunderbird! www.mozilla.com/thunderbird X-Debbugs-No-Ack: true User-Agent: Mutt/1.5.18 (2008-05-17) X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Subject: [PATCH] restructure code flow in grub-setup / setup() X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 10 May 2009 09:47:33 -0000 --a8Wt8u1KmwUX3Y2C Content-Type: text/plain; charset=us-ascii Content-Disposition: inline This restructures code flow in the setup() function to make it easier to work with. The current layout is really confusing; instead, I propose a "try-fail" approach, like: if (problem1) { grub_util_warn ("warning1"); goto unable_to_embed; } if (problem2) { grub_util_warn ("warning2"); goto unable_to_embed; } [...] To make my patch more readable I intentionally omitted indentation changes from it (which I'll add when committing, of course). -- Robert Millan The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all." --a8Wt8u1KmwUX3Y2C Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="grub_setup_flow.diff" 2009-05-10 Robert Millan * util/i386/pc/grub-setup.c (setup): Restructure code flow to make it easier to understand / work with. Index: util/i386/pc/grub-setup.c =================================================================== --- util/i386/pc/grub-setup.c (revision 2199) +++ util/i386/pc/grub-setup.c (working copy) @@ -310,17 +310,19 @@ dos_part, bsd_part); if (! dest_dev->disk->has_partitions) + { grub_util_warn ("Attempting to install GRUB to a partitionless disk. This is a BAD idea."); + goto unable_to_embed; + } if (dest_dev->disk->partition) + { grub_util_warn ("Attempting to install GRUB to a partition instead of the MBR. This is a BAD idea."); + goto unable_to_embed; + } - /* If the destination device can have partitions and it is the MBR, - try to embed the core image into after the MBR. */ - if (dest_dev->disk->has_partitions && ! dest_dev->disk->partition) - { /* Unlike root_dev, with dest_dev we're interested in the partition map even - if dest_dev itself is a whole disk. */ + if dest_dev itself is a whole disk. */ auto int NESTED_FUNC_ATTR identify_partmap (grub_disk_t disk, const grub_partition_t p); int NESTED_FUNC_ATTR identify_partmap (grub_disk_t disk __attribute__ ((unused)), @@ -330,16 +332,22 @@ return 1; } grub_partition_iterate (dest_dev->disk, identify_partmap); - + grub_partition_iterate (dest_dev->disk, (strcmp (dest_partmap, "pc_partition_map") ? find_usable_region_gpt : find_usable_region_msdos)); - - if (embed_region.end != embed_region.start) - embedding_area_exists = 1; - - /* If there is enough space... */ - if ((unsigned long) core_sectors <= embed_region.end - embed_region.start) - { + if (embed_region.end == embed_region.start) + { + grub_util_warn ("Embedding area is not present at all!"); + goto unable_to_embed; + } + + if ((unsigned long) core_sectors > embed_region.end - embed_region.start) + { + grub_util_warn ("Embedding area is too small for core.img."); + goto unable_to_embed; + } + + grub_util_info ("will embed the core image at sector 0x%llx", embed_region.start); *install_dos_part = grub_cpu_to_le32 (dos_part); @@ -374,16 +382,9 @@ grub_util_error ("%s", grub_errmsg); goto finish; - } - } - /* If we reached this point, it means we were unable to embed. */ +unable_to_embed: - if (embedding_area_exists) - grub_util_warn ("Embedding area is too small for core.img."); - else - grub_util_warn ("Embedding area is not present at all!"); - if (must_embed) grub_util_error ("Embedding is not possible, but this is required when " "the root device is on a RAID array or LVM volume."); --a8Wt8u1KmwUX3Y2C--