bootimg_partition.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: GPL-2.0-only
  5. #
  6. # DESCRIPTION
  7. # This implements the 'bootimg-partition' source plugin class for
  8. # 'wic'. The plugin creates an image of boot partition, copying over
  9. # files listed in IMAGE_BOOT_FILES bitbake variable.
  10. #
  11. # AUTHORS
  12. # Maciej Borzecki <maciej.borzecki (at] open-rnd.pl>
  13. #
  14. import logging
  15. import os
  16. import re
  17. from oe.bootfiles import get_boot_files
  18. from wic import WicError
  19. from wic.engine import get_custom_config
  20. from wic.pluginbase import SourcePlugin
  21. from wic.misc import exec_cmd, get_bitbake_var
  22. logger = logging.getLogger('wic')
  23. class BootimgPartitionPlugin(SourcePlugin):
  24. """
  25. Create an image of boot partition, copying over files
  26. listed in IMAGE_BOOT_FILES bitbake variable.
  27. """
  28. name = 'bootimg-partition'
  29. image_boot_files_var_name = 'IMAGE_BOOT_FILES'
  30. @classmethod
  31. def do_configure_partition(cls, part, source_params, cr, cr_workdir,
  32. oe_builddir, bootimg_dir, kernel_dir,
  33. native_sysroot):
  34. """
  35. Called before do_prepare_partition(), create u-boot specific boot config
  36. """
  37. hdddir = "%s/boot.%d" % (cr_workdir, part.lineno)
  38. install_cmd = "install -d %s" % hdddir
  39. exec_cmd(install_cmd)
  40. if not kernel_dir:
  41. kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
  42. if not kernel_dir:
  43. raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
  44. boot_files = None
  45. for (fmt, id) in (("_uuid-%s", part.uuid), ("_label-%s", part.label), (None, None)):
  46. if fmt:
  47. var = fmt % id
  48. else:
  49. var = ""
  50. boot_files = get_bitbake_var(cls.image_boot_files_var_name + var)
  51. if boot_files is not None:
  52. break
  53. if boot_files is None:
  54. raise WicError('No boot files defined, %s unset for entry #%d' % (cls.image_boot_files_var_name, part.lineno))
  55. logger.debug('Boot files: %s', boot_files)
  56. cls.install_task = get_boot_files(kernel_dir, boot_files)
  57. if source_params.get('loader') != "u-boot":
  58. return
  59. configfile = cr.ks.bootloader.configfile
  60. custom_cfg = None
  61. if configfile:
  62. custom_cfg = get_custom_config(configfile)
  63. if custom_cfg:
  64. # Use a custom configuration for extlinux.conf
  65. extlinux_conf = custom_cfg
  66. logger.debug("Using custom configuration file "
  67. "%s for extlinux.conf", configfile)
  68. else:
  69. raise WicError("configfile is specified but failed to "
  70. "get it from %s." % configfile)
  71. if not custom_cfg:
  72. # The kernel types supported by the sysboot of u-boot
  73. kernel_types = ["zImage", "Image", "fitImage", "uImage", "vmlinux"]
  74. has_dtb = False
  75. fdt_dir = '/'
  76. kernel_name = None
  77. # Find the kernel image name, from the highest precedence to lowest
  78. for image in kernel_types:
  79. for task in cls.install_task:
  80. src, dst = task
  81. if re.match(image, src):
  82. kernel_name = os.path.join('/', dst)
  83. break
  84. if kernel_name:
  85. break
  86. for task in cls.install_task:
  87. src, dst = task
  88. # We suppose that all the dtb are in the same directory
  89. if re.search(r'\.dtb', src) and fdt_dir == '/':
  90. has_dtb = True
  91. fdt_dir = os.path.join(fdt_dir, os.path.dirname(dst))
  92. break
  93. if not kernel_name:
  94. raise WicError('No kernel file found')
  95. # Compose the extlinux.conf
  96. extlinux_conf = "default Yocto\n"
  97. extlinux_conf += "label Yocto\n"
  98. extlinux_conf += " kernel %s\n" % kernel_name
  99. if has_dtb:
  100. extlinux_conf += " fdtdir %s\n" % fdt_dir
  101. bootloader = cr.ks.bootloader
  102. extlinux_conf += "append root=%s rootwait %s\n" \
  103. % (cr.rootdev, bootloader.append if bootloader.append else '')
  104. install_cmd = "install -d %s/extlinux/" % hdddir
  105. exec_cmd(install_cmd)
  106. cfg = open("%s/extlinux/extlinux.conf" % hdddir, "w")
  107. cfg.write(extlinux_conf)
  108. cfg.close()
  109. @classmethod
  110. def do_prepare_partition(cls, part, source_params, cr, cr_workdir,
  111. oe_builddir, bootimg_dir, kernel_dir,
  112. rootfs_dir, native_sysroot):
  113. """
  114. Called to do the actual content population for a partition i.e. it
  115. 'prepares' the partition to be incorporated into the image.
  116. In this case, does the following:
  117. - sets up a vfat partition
  118. - copies all files listed in IMAGE_BOOT_FILES variable
  119. """
  120. hdddir = "%s/boot.%d" % (cr_workdir, part.lineno)
  121. if not kernel_dir:
  122. kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
  123. if not kernel_dir:
  124. raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
  125. logger.debug('Kernel dir: %s', bootimg_dir)
  126. for task in cls.install_task:
  127. src_path, dst_path = task
  128. logger.debug('Install %s as %s', src_path, dst_path)
  129. install_cmd = "install -m 0644 -D %s %s" \
  130. % (os.path.join(kernel_dir, src_path),
  131. os.path.join(hdddir, dst_path))
  132. exec_cmd(install_cmd)
  133. logger.debug('Prepare boot partition using rootfs in %s', hdddir)
  134. part.prepare_rootfs(cr_workdir, oe_builddir, hdddir,
  135. native_sysroot, False)