bootimg-pcbios.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #
  2. # Copyright (c) 2014, Intel Corporation.
  3. #
  4. # SPDX-License-Identifier: GPL-2.0-only
  5. #
  6. # DESCRIPTION
  7. # This implements the 'bootimg-pcbios' source plugin class for 'wic'
  8. #
  9. # AUTHORS
  10. # Tom Zanussi <tom.zanussi (at] linux.intel.com>
  11. #
  12. import logging
  13. import os
  14. import re
  15. from wic import WicError
  16. from wic.engine import get_custom_config
  17. from wic.pluginbase import SourcePlugin
  18. from wic.misc import (exec_cmd, exec_native_cmd,
  19. get_bitbake_var, BOOTDD_EXTRA_SPACE)
  20. logger = logging.getLogger('wic')
  21. class BootimgPcbiosPlugin(SourcePlugin):
  22. """
  23. Create MBR boot partition and install syslinux on it.
  24. """
  25. name = 'bootimg-pcbios'
  26. @classmethod
  27. def _get_bootimg_dir(cls, bootimg_dir, dirname):
  28. """
  29. Check if dirname exists in default bootimg_dir or in STAGING_DIR.
  30. """
  31. staging_datadir = get_bitbake_var("STAGING_DATADIR")
  32. for result in (bootimg_dir, staging_datadir):
  33. if os.path.exists("%s/%s" % (result, dirname)):
  34. return result
  35. # STAGING_DATADIR is expanded with MLPREFIX if multilib is enabled
  36. # but dependency syslinux is still populated to original STAGING_DATADIR
  37. nonarch_datadir = re.sub('/[^/]*recipe-sysroot', '/recipe-sysroot', staging_datadir)
  38. if os.path.exists(os.path.join(nonarch_datadir, dirname)):
  39. return nonarch_datadir
  40. raise WicError("Couldn't find correct bootimg_dir, exiting")
  41. @classmethod
  42. def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir,
  43. bootimg_dir, kernel_dir, native_sysroot):
  44. """
  45. Called after all partitions have been prepared and assembled into a
  46. disk image. In this case, we install the MBR.
  47. """
  48. bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux')
  49. mbrfile = "%s/syslinux/" % bootimg_dir
  50. if creator.ptable_format == 'msdos':
  51. mbrfile += "mbr.bin"
  52. elif creator.ptable_format == 'gpt':
  53. mbrfile += "gptmbr.bin"
  54. else:
  55. raise WicError("Unsupported partition table: %s" %
  56. creator.ptable_format)
  57. if not os.path.exists(mbrfile):
  58. raise WicError("Couldn't find %s. If using the -e option, do you "
  59. "have the right MACHINE set in local.conf? If not, "
  60. "is the bootimg_dir path correct?" % mbrfile)
  61. full_path = creator._full_path(workdir, disk_name, "direct")
  62. logger.debug("Installing MBR on disk %s as %s with size %s bytes",
  63. disk_name, full_path, disk.min_size)
  64. dd_cmd = "dd if=%s of=%s conv=notrunc" % (mbrfile, full_path)
  65. exec_cmd(dd_cmd, native_sysroot)
  66. @classmethod
  67. def do_configure_partition(cls, part, source_params, creator, cr_workdir,
  68. oe_builddir, bootimg_dir, kernel_dir,
  69. native_sysroot):
  70. """
  71. Called before do_prepare_partition(), creates syslinux config
  72. """
  73. hdddir = "%s/hdd/boot" % cr_workdir
  74. install_cmd = "install -d %s" % hdddir
  75. exec_cmd(install_cmd)
  76. bootloader = creator.ks.bootloader
  77. custom_cfg = None
  78. if bootloader.configfile:
  79. custom_cfg = get_custom_config(bootloader.configfile)
  80. if custom_cfg:
  81. # Use a custom configuration for grub
  82. syslinux_conf = custom_cfg
  83. logger.debug("Using custom configuration file %s "
  84. "for syslinux.cfg", bootloader.configfile)
  85. else:
  86. raise WicError("configfile is specified but failed to "
  87. "get it from %s." % bootloader.configfile)
  88. if not custom_cfg:
  89. # Create syslinux configuration using parameters from wks file
  90. splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg")
  91. if os.path.exists(splash):
  92. splashline = "menu background splash.jpg"
  93. else:
  94. splashline = ""
  95. syslinux_conf = ""
  96. syslinux_conf += "PROMPT 0\n"
  97. syslinux_conf += "TIMEOUT " + str(bootloader.timeout) + "\n"
  98. syslinux_conf += "\n"
  99. syslinux_conf += "ALLOWOPTIONS 1\n"
  100. syslinux_conf += "SERIAL 0 115200\n"
  101. syslinux_conf += "\n"
  102. if splashline:
  103. syslinux_conf += "%s\n" % splashline
  104. syslinux_conf += "DEFAULT boot\n"
  105. syslinux_conf += "LABEL boot\n"
  106. kernel = "/vmlinuz"
  107. syslinux_conf += "KERNEL " + kernel + "\n"
  108. syslinux_conf += "APPEND label=boot root=%s %s\n" % \
  109. (creator.rootdev, bootloader.append)
  110. logger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg",
  111. cr_workdir)
  112. cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w")
  113. cfg.write(syslinux_conf)
  114. cfg.close()
  115. @classmethod
  116. def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
  117. oe_builddir, bootimg_dir, kernel_dir,
  118. rootfs_dir, native_sysroot):
  119. """
  120. Called to do the actual content population for a partition i.e. it
  121. 'prepares' the partition to be incorporated into the image.
  122. In this case, prepare content for legacy bios boot partition.
  123. """
  124. bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux')
  125. staging_kernel_dir = kernel_dir
  126. hdddir = "%s/hdd/boot" % cr_workdir
  127. kernel = get_bitbake_var("KERNEL_IMAGETYPE")
  128. if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1":
  129. if get_bitbake_var("INITRAMFS_IMAGE"):
  130. kernel = "%s-%s.bin" % \
  131. (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME"))
  132. cmds = ("install -m 0644 %s/%s %s/vmlinuz" %
  133. (staging_kernel_dir, kernel, hdddir),
  134. "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" %
  135. (bootimg_dir, hdddir),
  136. "install -m 0644 %s/syslinux/vesamenu.c32 %s/vesamenu.c32" %
  137. (bootimg_dir, hdddir),
  138. "install -m 444 %s/syslinux/libcom32.c32 %s/libcom32.c32" %
  139. (bootimg_dir, hdddir),
  140. "install -m 444 %s/syslinux/libutil.c32 %s/libutil.c32" %
  141. (bootimg_dir, hdddir))
  142. for install_cmd in cmds:
  143. exec_cmd(install_cmd)
  144. du_cmd = "du -bks %s" % hdddir
  145. out = exec_cmd(du_cmd)
  146. blocks = int(out.split()[0])
  147. extra_blocks = part.get_extra_block_count(blocks)
  148. if extra_blocks < BOOTDD_EXTRA_SPACE:
  149. extra_blocks = BOOTDD_EXTRA_SPACE
  150. blocks += extra_blocks
  151. logger.debug("Added %d extra blocks to %s to get to %d total blocks",
  152. extra_blocks, part.mountpoint, blocks)
  153. # dosfs image, created by mkdosfs
  154. bootimg = "%s/boot%s.img" % (cr_workdir, part.lineno)
  155. label = part.label if part.label else "boot"
  156. dosfs_cmd = "mkdosfs -n %s -i %s -S 512 -C %s %d" % \
  157. (label, part.fsuuid, bootimg, blocks)
  158. exec_native_cmd(dosfs_cmd, native_sysroot)
  159. mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir)
  160. exec_native_cmd(mcopy_cmd, native_sysroot)
  161. syslinux_cmd = "syslinux %s" % bootimg
  162. exec_native_cmd(syslinux_cmd, native_sysroot)
  163. chmod_cmd = "chmod 644 %s" % bootimg
  164. exec_cmd(chmod_cmd)
  165. du_cmd = "du -Lbks %s" % bootimg
  166. out = exec_cmd(du_cmd)
  167. bootimg_size = out.split()[0]
  168. part.size = int(bootimg_size)
  169. part.source_file = bootimg