2015-05-07 21:57:20 +00:00
|
|
|
#!/usr/bin/python3
|
2011-05-09 14:45:55 +00:00
|
|
|
# mkefiboot - a tool to make EFI boot images
|
2014-05-09 22:13:39 +00:00
|
|
|
#
|
2015-05-07 21:57:20 +00:00
|
|
|
# Copyright (C) 2011-2015 Red Hat, Inc.
|
2011-05-09 14:45:55 +00:00
|
|
|
#
|
|
|
|
# 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 2 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/>.
|
|
|
|
#
|
|
|
|
# Red Hat Author(s): Will Woods <wwoods@redhat.com>
|
|
|
|
|
2012-06-13 22:35:55 +00:00
|
|
|
import logging
|
|
|
|
logging.basicConfig()
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
2011-10-25 20:04:09 +00:00
|
|
|
import os, tempfile, argparse
|
2011-05-09 14:45:55 +00:00
|
|
|
from subprocess import check_call, PIPE
|
2011-05-10 02:56:14 +00:00
|
|
|
from pylorax.imgutils import mkdosimg, round_to_blocks, LoopDev, DMDev, dm_detach
|
2012-07-25 15:51:24 +00:00
|
|
|
from pylorax.imgutils import mkhfsimg, Mount, estimate_size
|
2011-09-07 16:05:29 +00:00
|
|
|
import struct, shutil, glob
|
2011-05-09 14:45:55 +00:00
|
|
|
|
|
|
|
def mkefiboot(bootdir, outfile, label):
|
|
|
|
'''Make an EFI boot image with the contents of bootdir in EFI/BOOT'''
|
2011-05-10 02:56:14 +00:00
|
|
|
mkdosimg(None, outfile, label=label, graft={'EFI/BOOT':bootdir})
|
2011-05-09 14:45:55 +00:00
|
|
|
|
2012-05-02 17:09:20 +00:00
|
|
|
def mkmacboot(bootdir, outfile, label, icon=None, product='Generic',
|
|
|
|
diskname=None):
|
2011-09-06 16:46:41 +00:00
|
|
|
'''Make an EFI boot image for Apple's EFI implementation'''
|
|
|
|
graft = {'EFI/BOOT':bootdir}
|
2012-05-02 17:09:20 +00:00
|
|
|
if icon and os.path.exists(icon):
|
2011-09-06 16:46:41 +00:00
|
|
|
graft['.VolumeIcon.icns'] = icon
|
2012-05-02 17:09:20 +00:00
|
|
|
if diskname and os.path.exists(diskname):
|
|
|
|
graft['EFI/BOOT/.disk_label'] = diskname
|
2012-07-25 15:51:24 +00:00
|
|
|
# everything winds up bein there twice...
|
|
|
|
size = estimate_size(bootdir, graft=graft) * 2
|
|
|
|
mkhfsimg(None, outfile, label=label, graft=graft, size=size)
|
2012-03-22 00:34:32 +00:00
|
|
|
macmunge(outfile, product)
|
2011-09-06 16:46:41 +00:00
|
|
|
|
|
|
|
# To make an HFS+ image bootable, we need to fill in parts of the
|
|
|
|
# HFSPlusVolumeHeader structure - specifically, finderInfo[0,1,5].
|
|
|
|
# For details, see Technical Note TN1150: HFS Plus Volume Format
|
|
|
|
# http://developer.apple.com/library/mac/#technotes/tn/tn1150.html
|
2012-03-22 00:34:32 +00:00
|
|
|
#
|
|
|
|
# Additionally, we want to do some fixups to make it play nicely with
|
|
|
|
# the startup disk preferences panel.
|
|
|
|
def macmunge(imgfile, product):
|
2011-09-06 16:46:41 +00:00
|
|
|
'''"bless" the EFI bootloader inside the given Mac EFI boot image, by
|
|
|
|
writing its inode info into the HFS+ volume header.'''
|
|
|
|
# Get the inode number for the boot image and its parent directory
|
2011-10-25 21:23:24 +00:00
|
|
|
with LoopDev(imgfile) as loopdev:
|
2011-09-06 16:46:41 +00:00
|
|
|
with Mount(loopdev) as mnt:
|
2014-10-07 14:44:32 +00:00
|
|
|
shim = glob.glob(os.path.join(mnt, 'EFI/BOOT/BOOT*.EFI'))[0]
|
2012-07-25 15:51:24 +00:00
|
|
|
loader = glob.glob(os.path.join(mnt,'EFI/BOOT/grub*.efi'))[0]
|
|
|
|
config = glob.glob(os.path.join(mnt,'EFI/*/grub*.cfg'))[0]
|
2011-09-06 16:46:41 +00:00
|
|
|
blessnode = os.stat(loader).st_ino
|
|
|
|
dirnode = os.stat(os.path.dirname(loader)).st_ino
|
2012-03-22 00:34:32 +00:00
|
|
|
with open(os.path.join(mnt,'mach_kernel'), 'w') as kernel:
|
|
|
|
kernel.write('Dummy kernel for booting')
|
|
|
|
sysdir = os.path.join(mnt,'System/Library/CoreServices/')
|
|
|
|
os.makedirs(sysdir)
|
|
|
|
with open(os.path.join(sysdir,'SystemVersion.plist'), 'w') as plist:
|
|
|
|
plist.write('''<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
|
|
<plist version="1.0">
|
|
|
|
<dict>
|
|
|
|
<key>ProductBuildVersion</key>
|
|
|
|
<string></string>
|
|
|
|
<key>ProductName</key>
|
|
|
|
<string>Linux</string>
|
|
|
|
<key>ProductVersion</key>
|
|
|
|
<string>%s</string>
|
|
|
|
</dict>
|
|
|
|
</plist>
|
|
|
|
''' % (product,))
|
2012-07-25 15:51:24 +00:00
|
|
|
shutil.copy(shim, os.path.join(sysdir,'boot.efi'))
|
|
|
|
shutil.copy(loader, sysdir)
|
|
|
|
shutil.copy(config, sysdir)
|
2011-09-06 16:46:41 +00:00
|
|
|
# format data properly (big-endian UInt32)
|
|
|
|
nodedata = struct.pack(">i", blessnode)
|
|
|
|
dirdata = struct.pack(">i", dirnode)
|
|
|
|
# Write it to the volume header
|
2011-11-01 17:35:24 +00:00
|
|
|
with open(imgfile, "r+b") as img:
|
2011-09-06 16:46:41 +00:00
|
|
|
img.seek(0x450) # HFSPlusVolumeHeader->finderInfo
|
|
|
|
img.write(dirdata) # finderInfo[0]
|
|
|
|
img.write(nodedata) # finderInfo[1]
|
|
|
|
img.seek(0x464) #
|
|
|
|
img.write(dirdata) # finderInfo[5]
|
|
|
|
|
2011-05-09 14:45:55 +00:00
|
|
|
def mkefidisk(efiboot, outfile):
|
|
|
|
'''Make a bootable EFI disk image out of the given EFI boot image.'''
|
2012-07-25 15:52:46 +00:00
|
|
|
# pjones sez: "17408 is the size of the GPT tables parted creates,
|
|
|
|
# but there are two of them."
|
|
|
|
partsize = os.path.getsize(efiboot) + 17408 * 2
|
|
|
|
disksize = round_to_blocks(2 * 17408 + partsize, 512)
|
2011-05-09 14:45:55 +00:00
|
|
|
with LoopDev(outfile, disksize) as loopdev:
|
|
|
|
with DMDev(loopdev, disksize) as dmdev:
|
|
|
|
check_call(["parted", "--script", "/dev/mapper/%s" % dmdev,
|
|
|
|
"mklabel", "gpt",
|
|
|
|
"unit", "b",
|
2012-07-25 15:52:46 +00:00
|
|
|
"mkpart", "'EFI System Partition'", "fat32", "34816", str(partsize),
|
2011-05-09 14:45:55 +00:00
|
|
|
"set", "1", "boot", "on"], stdout=PIPE, stderr=PIPE)
|
2011-08-30 17:10:13 +00:00
|
|
|
partdev = "/dev/mapper/{0}p1".format(dmdev)
|
2011-05-09 14:45:55 +00:00
|
|
|
with open(efiboot, "rb") as infile:
|
2011-08-30 17:10:13 +00:00
|
|
|
with open(partdev, "wb") as outfile:
|
2011-05-09 14:45:55 +00:00
|
|
|
outfile.write(infile.read())
|
|
|
|
dm_detach(dmdev+"p1")
|
|
|
|
|
2014-05-09 00:22:14 +00:00
|
|
|
def main():
|
2011-10-25 20:04:09 +00:00
|
|
|
parser = argparse.ArgumentParser(description="Make an EFI boot image from the given directory.")
|
2012-06-13 22:35:55 +00:00
|
|
|
parser.add_argument("--debug", action="store_const", const=logging.DEBUG,
|
|
|
|
dest="loglevel", default=log.getEffectiveLevel(),
|
|
|
|
help="print debugging info")
|
2011-10-25 20:04:09 +00:00
|
|
|
parser.add_argument("-d", "--disk", action="store_true",
|
|
|
|
help="make a full EFI disk image (including partition table)")
|
|
|
|
parser.add_argument("-a", "--apple", action="store_const", const="apple",
|
|
|
|
dest="imgtype", default="default",
|
|
|
|
help="make an Apple EFI image (use hfs+, bless bootloader)")
|
|
|
|
parser.add_argument("-l", "--label", default="EFI",
|
|
|
|
help="filesystem label to use (default: %(default)s)")
|
|
|
|
parser.add_argument("-i", "--icon", metavar="ICONFILE",
|
|
|
|
help="icon file to include (for Apple EFI image)")
|
2012-05-02 17:09:20 +00:00
|
|
|
parser.add_argument("-n", "--diskname", metavar="DISKNAME",
|
|
|
|
help="disk name image to include (for Apple EFI image)")
|
2012-03-22 00:34:32 +00:00
|
|
|
parser.add_argument("-p", "--product", metavar="PRODUCT",
|
|
|
|
help="product name to use (for Apple EFI image)")
|
2011-10-25 20:04:09 +00:00
|
|
|
parser.add_argument("bootdir", metavar="EFIBOOTDIR",
|
|
|
|
help="input directory (will become /EFI/BOOT in the image)")
|
|
|
|
parser.add_argument("outfile", metavar="OUTPUTFILE",
|
|
|
|
help="output file to write")
|
|
|
|
opt = parser.parse_args()
|
2012-06-13 22:35:55 +00:00
|
|
|
# logging
|
|
|
|
log.setLevel(opt.loglevel)
|
2011-05-09 14:45:55 +00:00
|
|
|
# sanity checks
|
2011-10-25 20:04:09 +00:00
|
|
|
if not os.path.isdir(opt.bootdir):
|
|
|
|
parser.error("%s is not a directory" % opt.bootdir)
|
2011-05-09 14:45:55 +00:00
|
|
|
if os.getuid() > 0:
|
|
|
|
parser.error("need root permissions")
|
2011-09-06 16:46:41 +00:00
|
|
|
if opt.icon and not opt.imgtype == "apple":
|
2015-05-07 21:57:20 +00:00
|
|
|
print("Warning: --icon is only useful for Apple EFI images")
|
2012-05-02 17:09:20 +00:00
|
|
|
if opt.diskname and not opt.imgtype == "apple":
|
2015-05-07 21:57:20 +00:00
|
|
|
print("Warning: --diskname is only useful for Apple EFI images")
|
2011-05-09 14:45:55 +00:00
|
|
|
# do the thing!
|
2011-09-07 16:05:29 +00:00
|
|
|
if opt.imgtype == "apple":
|
2012-05-02 17:09:20 +00:00
|
|
|
mkmacboot(opt.bootdir, opt.outfile, opt.label, opt.icon, opt.product,
|
|
|
|
opt.diskname)
|
2011-09-06 16:46:41 +00:00
|
|
|
else:
|
2011-10-25 20:04:09 +00:00
|
|
|
mkefiboot(opt.bootdir, opt.outfile, opt.label)
|
2011-05-09 14:45:55 +00:00
|
|
|
if opt.disk:
|
|
|
|
efiboot = tempfile.NamedTemporaryFile(prefix="mkefiboot.").name
|
2011-10-25 20:04:09 +00:00
|
|
|
shutil.move(opt.outfile, efiboot)
|
|
|
|
mkefidisk(efiboot, opt.outfile)
|
2014-05-09 00:22:14 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|