xref: /OK3568_Linux_fs/yocto/poky/scripts/lib/wic/plugins/source/bootimg-biosplusefi.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#
2*4882a593Smuzhiyun# This program is free software; you can redistribute it and/or modify
3*4882a593Smuzhiyun# it under the terms of the GNU General Public License version 2 as
4*4882a593Smuzhiyun# published by the Free Software Foundation.
5*4882a593Smuzhiyun#
6*4882a593Smuzhiyun# This program is distributed in the hope that it will be useful,
7*4882a593Smuzhiyun# but WITHOUT ANY WARRANTY; without even the implied warranty of
8*4882a593Smuzhiyun# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9*4882a593Smuzhiyun# GNU General Public License for more details.
10*4882a593Smuzhiyun#
11*4882a593Smuzhiyun# You should have received a copy of the GNU General Public License along
12*4882a593Smuzhiyun# with this program; if not, write to the Free Software Foundation, Inc.,
13*4882a593Smuzhiyun# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14*4882a593Smuzhiyun#
15*4882a593Smuzhiyun# DESCRIPTION
16*4882a593Smuzhiyun# This implements the 'bootimg-biosplusefi' source plugin class for 'wic'
17*4882a593Smuzhiyun#
18*4882a593Smuzhiyun# AUTHORS
19*4882a593Smuzhiyun# William Bourque <wbourque [at) gmail.com>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyunimport types
22*4882a593Smuzhiyun
23*4882a593Smuzhiyunfrom wic.pluginbase import SourcePlugin
24*4882a593Smuzhiyunfrom importlib.machinery import SourceFileLoader
25*4882a593Smuzhiyun
26*4882a593Smuzhiyunclass BootimgBiosPlusEFIPlugin(SourcePlugin):
27*4882a593Smuzhiyun    """
28*4882a593Smuzhiyun    Create MBR + EFI boot partition
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun    This plugin creates a boot partition that contains both
31*4882a593Smuzhiyun    legacy BIOS and EFI content. It will be able to boot from both.
32*4882a593Smuzhiyun    This is useful when managing PC fleet with some older machines
33*4882a593Smuzhiyun    without EFI support.
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun    Note it is possible to create an image that can boot from both
36*4882a593Smuzhiyun    legacy BIOS and EFI by defining two partitions : one with arg
37*4882a593Smuzhiyun    --source bootimg-efi  and another one with --source bootimg-pcbios.
38*4882a593Smuzhiyun    However, this method has the obvious downside that it requires TWO
39*4882a593Smuzhiyun    partitions to be created on the storage device.
40*4882a593Smuzhiyun    Both partitions will also be marked as "bootable" which does not work on
41*4882a593Smuzhiyun    most BIOS, has BIOS often uses the "bootable" flag to determine
42*4882a593Smuzhiyun    what to boot. If you have such a BIOS, you need to manually remove the
43*4882a593Smuzhiyun    "bootable" flag from the EFI partition for the drive to be bootable.
44*4882a593Smuzhiyun    Having two partitions also seems to confuse wic : the content of
45*4882a593Smuzhiyun    the first partition will be duplicated into the second, even though it
46*4882a593Smuzhiyun    will not be used at all.
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun    Also, unlike "isoimage-isohybrid" that also does BIOS and EFI, this plugin
49*4882a593Smuzhiyun    allows you to have more than only a single rootfs partitions and does
50*4882a593Smuzhiyun    not turn the rootfs into an initramfs RAM image.
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun    This plugin is made to put everything into a single /boot partition so it
53*4882a593Smuzhiyun    does not have the limitations listed above.
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun    The plugin is made so it does tries not to reimplement what's already
56*4882a593Smuzhiyun    been done in other plugins; as such it imports "bootimg-pcbios"
57*4882a593Smuzhiyun    and "bootimg-efi".
58*4882a593Smuzhiyun    Plugin "bootimg-pcbios" is used to generate legacy BIOS boot.
59*4882a593Smuzhiyun    Plugin "bootimg-efi" is used to generate the UEFI boot. Note that it
60*4882a593Smuzhiyun    requires a --sourceparams argument to know which loader to use; refer
61*4882a593Smuzhiyun    to "bootimg-efi" code/documentation for the list of loader.
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun    Imports are handled with "SourceFileLoader" from importlib as it is
64*4882a593Smuzhiyun    otherwise very difficult to import module that has hyphen "-" in their
65*4882a593Smuzhiyun    filename.
66*4882a593Smuzhiyun    The SourcePlugin() methods used in the plugins (do_install_disk,
67*4882a593Smuzhiyun    do_configure_partition, do_prepare_partition) are then called on both,
68*4882a593Smuzhiyun    beginning by "bootimg-efi".
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun    Plugin options, such as "--sourceparams" can still be passed to a
71*4882a593Smuzhiyun    plugin, as long they does not cause issue in the other plugin.
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun    Example wic configuration:
74*4882a593Smuzhiyun    part /boot --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\\
75*4882a593Smuzhiyun               --ondisk sda --label os_boot --active --align 1024 --use-uuid
76*4882a593Smuzhiyun    """
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun    name = 'bootimg-biosplusefi'
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun    __PCBIOS_MODULE_NAME = "bootimg-pcbios"
81*4882a593Smuzhiyun    __EFI_MODULE_NAME = "bootimg-efi"
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun    __imgEFIObj = None
84*4882a593Smuzhiyun    __imgBiosObj = None
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun    @classmethod
87*4882a593Smuzhiyun    def __init__(cls):
88*4882a593Smuzhiyun        """
89*4882a593Smuzhiyun        Constructor (init)
90*4882a593Smuzhiyun        """
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun        # XXX
93*4882a593Smuzhiyun        # For some reasons, __init__ constructor is never called.
94*4882a593Smuzhiyun        # Something to do with how pluginbase works?
95*4882a593Smuzhiyun        cls.__instanciateSubClasses()
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun    @classmethod
98*4882a593Smuzhiyun    def __instanciateSubClasses(cls):
99*4882a593Smuzhiyun        """
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun        """
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun        # Import bootimg-pcbios (class name "BootimgPcbiosPlugin")
104*4882a593Smuzhiyun        modulePath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
105*4882a593Smuzhiyun                                  cls.__PCBIOS_MODULE_NAME + ".py")
106*4882a593Smuzhiyun        loader = SourceFileLoader(cls.__PCBIOS_MODULE_NAME, modulePath)
107*4882a593Smuzhiyun        mod = types.ModuleType(loader.name)
108*4882a593Smuzhiyun        loader.exec_module(mod)
109*4882a593Smuzhiyun        cls.__imgBiosObj = mod.BootimgPcbiosPlugin()
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun        # Import bootimg-efi (class name "BootimgEFIPlugin")
112*4882a593Smuzhiyun        modulePath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
113*4882a593Smuzhiyun                                  cls.__EFI_MODULE_NAME + ".py")
114*4882a593Smuzhiyun        loader = SourceFileLoader(cls.__EFI_MODULE_NAME, modulePath)
115*4882a593Smuzhiyun        mod = types.ModuleType(loader.name)
116*4882a593Smuzhiyun        loader.exec_module(mod)
117*4882a593Smuzhiyun        cls.__imgEFIObj = mod.BootimgEFIPlugin()
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun    @classmethod
120*4882a593Smuzhiyun    def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir,
121*4882a593Smuzhiyun                        bootimg_dir, kernel_dir, native_sysroot):
122*4882a593Smuzhiyun        """
123*4882a593Smuzhiyun        Called after all partitions have been prepared and assembled into a
124*4882a593Smuzhiyun        disk image.
125*4882a593Smuzhiyun        """
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun        if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ):
128*4882a593Smuzhiyun            cls.__instanciateSubClasses()
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun        cls.__imgEFIObj.do_install_disk(
131*4882a593Smuzhiyun            disk,
132*4882a593Smuzhiyun            disk_name,
133*4882a593Smuzhiyun            creator,
134*4882a593Smuzhiyun            workdir,
135*4882a593Smuzhiyun            oe_builddir,
136*4882a593Smuzhiyun            bootimg_dir,
137*4882a593Smuzhiyun            kernel_dir,
138*4882a593Smuzhiyun            native_sysroot)
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun        cls.__imgBiosObj.do_install_disk(
141*4882a593Smuzhiyun            disk,
142*4882a593Smuzhiyun            disk_name,
143*4882a593Smuzhiyun            creator,
144*4882a593Smuzhiyun            workdir,
145*4882a593Smuzhiyun            oe_builddir,
146*4882a593Smuzhiyun            bootimg_dir,
147*4882a593Smuzhiyun            kernel_dir,
148*4882a593Smuzhiyun            native_sysroot)
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun    @classmethod
151*4882a593Smuzhiyun    def do_configure_partition(cls, part, source_params, creator, cr_workdir,
152*4882a593Smuzhiyun                               oe_builddir, bootimg_dir, kernel_dir,
153*4882a593Smuzhiyun                               native_sysroot):
154*4882a593Smuzhiyun        """
155*4882a593Smuzhiyun        Called before do_prepare_partition()
156*4882a593Smuzhiyun        """
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun        if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ):
159*4882a593Smuzhiyun            cls.__instanciateSubClasses()
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun        cls.__imgEFIObj.do_configure_partition(
162*4882a593Smuzhiyun            part,
163*4882a593Smuzhiyun            source_params,
164*4882a593Smuzhiyun            creator,
165*4882a593Smuzhiyun            cr_workdir,
166*4882a593Smuzhiyun            oe_builddir,
167*4882a593Smuzhiyun            bootimg_dir,
168*4882a593Smuzhiyun            kernel_dir,
169*4882a593Smuzhiyun            native_sysroot)
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun        cls.__imgBiosObj.do_configure_partition(
172*4882a593Smuzhiyun            part,
173*4882a593Smuzhiyun            source_params,
174*4882a593Smuzhiyun            creator,
175*4882a593Smuzhiyun            cr_workdir,
176*4882a593Smuzhiyun            oe_builddir,
177*4882a593Smuzhiyun            bootimg_dir,
178*4882a593Smuzhiyun            kernel_dir,
179*4882a593Smuzhiyun            native_sysroot)
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun    @classmethod
182*4882a593Smuzhiyun    def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
183*4882a593Smuzhiyun                             oe_builddir, bootimg_dir, kernel_dir,
184*4882a593Smuzhiyun                             rootfs_dir, native_sysroot):
185*4882a593Smuzhiyun        """
186*4882a593Smuzhiyun        Called to do the actual content population for a partition i.e. it
187*4882a593Smuzhiyun        'prepares' the partition to be incorporated into the image.
188*4882a593Smuzhiyun        """
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun        if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ):
191*4882a593Smuzhiyun            cls.__instanciateSubClasses()
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun        cls.__imgEFIObj.do_prepare_partition(
194*4882a593Smuzhiyun            part,
195*4882a593Smuzhiyun            source_params,
196*4882a593Smuzhiyun            creator,
197*4882a593Smuzhiyun            cr_workdir,
198*4882a593Smuzhiyun            oe_builddir,
199*4882a593Smuzhiyun            bootimg_dir,
200*4882a593Smuzhiyun            kernel_dir,
201*4882a593Smuzhiyun            rootfs_dir,
202*4882a593Smuzhiyun            native_sysroot)
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun        cls.__imgBiosObj.do_prepare_partition(
205*4882a593Smuzhiyun            part,
206*4882a593Smuzhiyun            source_params,
207*4882a593Smuzhiyun            creator,
208*4882a593Smuzhiyun            cr_workdir,
209*4882a593Smuzhiyun            oe_builddir,
210*4882a593Smuzhiyun            bootimg_dir,
211*4882a593Smuzhiyun            kernel_dir,
212*4882a593Smuzhiyun            rootfs_dir,
213*4882a593Smuzhiyun            native_sysroot)
214