diff options
author | Vincent Davis Jr <vince@underview.tech> | 2025-08-14 00:25:49 -0400 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2025-08-14 10:31:11 +0100 |
commit | 7f522461d2ade980a1efcea290e4c879c92939aa (patch) | |
tree | 408cecb6cc3c8b00111bcf1008f275fb0fb6c574 /scripts/lib | |
parent | 8b64e77f0025ecbe73931d31030f6dd55dffe9d6 (diff) | |
download | poky-7f522461d2ade980a1efcea290e4c879c92939aa.tar.gz |
bootimg_pcbios: add funcs to configure booting with grub
Functions added, but not executed during
wic image creation include:
_get_staging_libdir
* Finds target lib directory if for some
reason STAGING_LIBDIR isn't set.
_do_configure_grub
* Will search for a grub configuration passed via
bootloader --configfile. If not found build a
default one which searches for partition that
contains the given the kernel name via grub
search module.
_do_prepare_grub
1. Sets default values for GRUB_MKIMAGE_FORMAT_PC
and GRUB_PREFIX_PATH if none specified. Both
variables are required by grub-mkimage.
* GRUB_MKIMAGE_FORMAT_PC is used to define
target platform.
* GRUB_PREFIX_PATH is used to define which
directory grub config and modules are going
to reside in.
2. Generates grub config to embed into core.img.
This config is used to search for partition
containing grub config.
3. Creates a custom core.img or grub stage 1.5
with an embedded grub config.
4. Copies all the target built grub modules into
GRUB_PREFIX_PATH directory.
5. Creates boot partition
_do_install_grub
1. dd target platform specific boot.img to the first
0-440 bytes of the resulting wic image. dd grub
stage 1 to wic image. If this wics plugin is used
with GPT as partition table format and grub selected
as bootloader it's more than likely for grub hybrid
booting because bootimg_efi plugin should and more
than likely will be used in that case. So, boot.img
may be dd regardless if partition table format is
GPT or MBR.
2. dd custom core.img (grub stage 1.5) with embedded
configuration to the resulting wic image starting
at byte 512 up to sizeof(core.img).
3. Both boot.img and core.img are required for legacy
bios boot. See grub Wiki for more details on
boot.img and core.img.
https://en.wikipedia.org/wiki/GNU_GRUB
Commit also imports python modules required by the
above implemented functions.
(From OE-Core rev: 27c56962f47303cf49a4cf641e85239e4d7779b5)
Signed-off-by: Vincent Davis Jr <vince@underview.tech>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/lib')
-rw-r--r-- | scripts/lib/wic/plugins/source/bootimg_pcbios.py | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/scripts/lib/wic/plugins/source/bootimg_pcbios.py b/scripts/lib/wic/plugins/source/bootimg_pcbios.py index a4fabec0ae..f50a5ae0e2 100644 --- a/scripts/lib/wic/plugins/source/bootimg_pcbios.py +++ b/scripts/lib/wic/plugins/source/bootimg_pcbios.py | |||
@@ -13,7 +13,9 @@ | |||
13 | import logging | 13 | import logging |
14 | import os | 14 | import os |
15 | import re | 15 | import re |
16 | import shutil | ||
16 | 17 | ||
18 | from glob import glob | ||
17 | from wic import WicError | 19 | from wic import WicError |
18 | from wic.engine import get_custom_config | 20 | from wic.engine import get_custom_config |
19 | from wic.pluginbase import SourcePlugin | 21 | from wic.pluginbase import SourcePlugin |
@@ -73,6 +75,27 @@ class BootimgPcbiosPlugin(SourcePlugin): | |||
73 | kernel_dir, native_sysroot) | 75 | kernel_dir, native_sysroot) |
74 | 76 | ||
75 | @classmethod | 77 | @classmethod |
78 | def _get_staging_libdir(cls): | ||
79 | """ | ||
80 | For unknown reasons when running test with poky | ||
81 | STAGING_LIBDIR gets unset when wic create is executed. | ||
82 | Bellow is a hack to determine what STAGING_LIBDIR should | ||
83 | be if not specified. | ||
84 | """ | ||
85 | |||
86 | staging_libdir = get_bitbake_var('STAGING_LIBDIR') | ||
87 | staging_dir_target = get_bitbake_var('STAGING_DIR_TARGET') | ||
88 | |||
89 | if not staging_libdir: | ||
90 | staging_libdir = '%s/usr/lib64' % staging_dir_target | ||
91 | if not os.path.isdir(staging_libdir): | ||
92 | staging_libdir = '%s/usr/lib32' % staging_dir_target | ||
93 | if not os.path.isdir(staging_libdir): | ||
94 | staging_libdir = '%s/usr/lib' % staging_dir_target | ||
95 | |||
96 | return staging_libdir | ||
97 | |||
98 | @classmethod | ||
76 | def _get_bootloader_config(cls, bootloader, loader): | 99 | def _get_bootloader_config(cls, bootloader, loader): |
77 | custom_cfg = None | 100 | custom_cfg = None |
78 | 101 | ||
@@ -243,3 +266,147 @@ class BootimgPcbiosPlugin(SourcePlugin): | |||
243 | 266 | ||
244 | dd_cmd = "dd if=%s of=%s conv=notrunc" % (mbrfile, full_path) | 267 | dd_cmd = "dd if=%s of=%s conv=notrunc" % (mbrfile, full_path) |
245 | exec_cmd(dd_cmd, native_sysroot) | 268 | exec_cmd(dd_cmd, native_sysroot) |
269 | |||
270 | @classmethod | ||
271 | def _do_configure_grub(cls, part, creator, cr_workdir): | ||
272 | hdddir = "%s/hdd" % cr_workdir | ||
273 | bootloader = creator.ks.bootloader | ||
274 | |||
275 | grub_conf = cls._get_bootloader_config(bootloader, 'grub') | ||
276 | |||
277 | grub_prefix_path = get_bitbake_var('GRUB_PREFIX_PATH') | ||
278 | if not grub_prefix_path: | ||
279 | grub_prefix_path = '/boot/grub' | ||
280 | |||
281 | grub_path = "%s/%s" %(hdddir, grub_prefix_path) | ||
282 | install_cmd = "install -d %s" % grub_path | ||
283 | exec_cmd(install_cmd) | ||
284 | |||
285 | if not grub_conf: | ||
286 | # Set a default timeout if none specified to avoid | ||
287 | # 'None' being the value placed within the configuration | ||
288 | # file. | ||
289 | if not bootloader.timeout: | ||
290 | bootloader.timeout = 500 | ||
291 | |||
292 | # Set a default kernel params string if none specified | ||
293 | # to avoid 'None' being the value placed within the | ||
294 | # configuration file. | ||
295 | if not bootloader.append: | ||
296 | bootloader.append = "rootwait rootfstype=%s " % (part.fstype) | ||
297 | bootloader.append += "console=ttyS0,115200 console=tty0" | ||
298 | |||
299 | kernel = "/boot/" + get_bitbake_var("KERNEL_IMAGETYPE") | ||
300 | |||
301 | grub_conf = 'serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1\n' | ||
302 | grub_conf += 'set gfxmode=auto\n' | ||
303 | grub_conf += 'set gfxpayload=keep\n\n' | ||
304 | grub_conf += 'set default=0\n\n' | ||
305 | grub_conf += '# Boot automatically after %d secs.\n' % (bootloader.timeout) | ||
306 | grub_conf += 'set timeout=%d\n\n' % (bootloader.timeout) | ||
307 | grub_conf += 'menuentry \'default\' {\n' | ||
308 | grub_conf += '\tsearch --no-floppy --set=root --file %s\n' % (kernel) | ||
309 | grub_conf += '\tprobe --set partuuid --part-uuid ($root)\n' | ||
310 | grub_conf += '\tlinux %s root=PARTUUID=$partuuid %s\n}\n' % \ | ||
311 | (kernel, bootloader.append) | ||
312 | |||
313 | logger.debug("Writing grub config %s/grub.cfg", grub_path) | ||
314 | cfg = open("%s/grub.cfg" % grub_path, "w") | ||
315 | cfg.write(grub_conf) | ||
316 | cfg.close() | ||
317 | |||
318 | @classmethod | ||
319 | def _do_prepare_grub(cls, part, cr_workdir, oe_builddir, | ||
320 | kernel_dir, rootfs_dir, native_sysroot): | ||
321 | """ | ||
322 | 1. Generate embed.cfg that'll later be embedded into core.img. | ||
323 | So, that core.img knows where to search for grub.cfg. | ||
324 | 2. Generate core.img or grub stage 1.5. | ||
325 | 3. Copy modules into partition. | ||
326 | 4. Create partition rootfs file. | ||
327 | """ | ||
328 | |||
329 | hdddir = "%s/hdd" % cr_workdir | ||
330 | |||
331 | copy_types = [ '*.mod', '*.o', '*.lst' ] | ||
332 | |||
333 | builtin_modules = 'boot linux ext2 fat serial part_msdos part_gpt \ | ||
334 | normal multiboot probe biosdisk msdospart configfile search loadenv test' | ||
335 | |||
336 | staging_libdir = cls._get_staging_libdir() | ||
337 | |||
338 | grub_format = get_bitbake_var('GRUB_MKIMAGE_FORMAT_PC') | ||
339 | if not grub_format: | ||
340 | grub_format = 'i386-pc' | ||
341 | |||
342 | grub_prefix_path = get_bitbake_var('GRUB_PREFIX_PATH') | ||
343 | if not grub_prefix_path: | ||
344 | grub_prefix_path = '/boot/grub' | ||
345 | |||
346 | grub_path = "%s/%s" %(hdddir, grub_prefix_path) | ||
347 | core_img = '%s/grub-bios-core.img' % (kernel_dir) | ||
348 | grub_mods_path = '%s/grub/%s' % (staging_libdir, grub_format) | ||
349 | |||
350 | # Generate embedded grub config | ||
351 | embed_cfg_str = 'search.file %s/grub.cfg root\n' % (grub_prefix_path) | ||
352 | embed_cfg_str += 'set prefix=($root)%s\n' % (grub_prefix_path) | ||
353 | embed_cfg_str += 'configfile ($root)%s/grub.cfg\n' % (grub_prefix_path) | ||
354 | cfg = open('%s/embed.cfg' % (kernel_dir), 'w+') | ||
355 | cfg.write(embed_cfg_str) | ||
356 | cfg.close() | ||
357 | |||
358 | # core.img doesn't get included into boot partition | ||
359 | # it's later dd onto the resulting wic image. | ||
360 | grub_mkimage = 'grub-mkimage \ | ||
361 | --prefix=%s \ | ||
362 | --format=%s \ | ||
363 | --config=%s/embed.cfg \ | ||
364 | --directory=%s \ | ||
365 | --output=%s %s' % \ | ||
366 | (grub_prefix_path, grub_format, kernel_dir, | ||
367 | grub_mods_path, core_img, builtin_modules) | ||
368 | exec_native_cmd(grub_mkimage, native_sysroot) | ||
369 | |||
370 | # Copy grub modules | ||
371 | install_dir = '%s/%s/%s' % (hdddir, grub_prefix_path, grub_format) | ||
372 | os.makedirs(install_dir, exist_ok=True) | ||
373 | |||
374 | for ctype in copy_types: | ||
375 | files = glob('%s/grub/%s/%s' % \ | ||
376 | (staging_libdir, grub_format, ctype)) | ||
377 | for file in files: | ||
378 | shutil.copy2(file, install_dir, follow_symlinks=True) | ||
379 | |||
380 | # Create boot partition | ||
381 | logger.debug('Prepare partition using rootfs in %s', hdddir) | ||
382 | part.prepare_rootfs(cr_workdir, oe_builddir, hdddir, | ||
383 | native_sysroot, False) | ||
384 | |||
385 | @classmethod | ||
386 | def _do_install_grub(cls, creator, kernel_dir, | ||
387 | native_sysroot, full_path): | ||
388 | core_img = '%s/grub-bios-core.img' % (kernel_dir) | ||
389 | |||
390 | staging_libdir = cls._get_staging_libdir() | ||
391 | |||
392 | grub_format = get_bitbake_var('GRUB_MKIMAGE_FORMAT_PC') | ||
393 | if not grub_format: | ||
394 | grub_format = 'i386-pc' | ||
395 | |||
396 | boot_img = '%s/grub/%s/boot.img' % (staging_libdir, grub_format) | ||
397 | if not os.path.exists(boot_img): | ||
398 | raise WicError("Couldn't find %s. Did you include " | ||
399 | "do_image_wic[depends] += \"grub:do_populate_sysroot\" " | ||
400 | "in your image recipe" % boot_img) | ||
401 | |||
402 | # Install boot.img or grub stage 1 | ||
403 | dd_cmd = "dd if=%s of=%s conv=notrunc bs=1 seek=0 count=440" % (boot_img, full_path) | ||
404 | exec_cmd(dd_cmd, native_sysroot) | ||
405 | |||
406 | if creator.ptable_format == 'msdos': | ||
407 | # Install core.img or grub stage 1.5 | ||
408 | dd_cmd = "dd if=%s of=%s conv=notrunc bs=1 seek=512" % (core_img, full_path) | ||
409 | exec_cmd(dd_cmd, native_sysroot) | ||
410 | else: | ||
411 | raise WicError("Unsupported partition table: %s" % | ||
412 | creator.ptable_format) | ||