bootimg-partition.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 glob import glob
  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. @classmethod
  30. def do_configure_partition(cls, part, source_params, cr, cr_workdir,
  31. oe_builddir, bootimg_dir, kernel_dir,
  32. native_sysroot):
  33. """
  34. Called before do_prepare_partition(), create u-boot specific boot config
  35. """
  36. hdddir = "%s/boot.%d" % (cr_workdir, part.lineno)
  37. install_cmd = "install -d %s" % hdddir
  38. exec_cmd(install_cmd)
  39. if not kernel_dir:
  40. kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
  41. if not kernel_dir:
  42. raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
  43. boot_files = None
  44. for (fmt, id) in (("_uuid-%s", part.uuid), ("_label-%s", part.label), (None, None)):
  45. if fmt:
  46. var = fmt % id
  47. else:
  48. var = ""
  49. boot_files = get_bitbake_var("IMAGE_BOOT_FILES" + var)
  50. if boot_files is not None:
  51. break
  52. if boot_files is None:
  53. raise WicError('No boot files defined, IMAGE_BOOT_FILES unset for entry #%d' % part.lineno)
  54. logger.debug('Boot files: %s', boot_files)
  55. # list of tuples (src_name, dst_name)
  56. deploy_files = []
  57. for src_entry in re.findall(r'[\w;\-\./\*]+', boot_files):
  58. if ';' in src_entry:
  59. dst_entry = tuple(src_entry.split(';'))
  60. if not dst_entry[0] or not dst_entry[1]:
  61. raise WicError('Malformed boot file entry: %s' % src_entry)
  62. else:
  63. dst_entry = (src_entry, src_entry)
  64. logger.debug('Destination entry: %r', dst_entry)
  65. deploy_files.append(dst_entry)
  66. cls.install_task = [];
  67. for deploy_entry in deploy_files:
  68. src, dst = deploy_entry
  69. if '*' in src:
  70. # by default install files under their basename
  71. entry_name_fn = os.path.basename
  72. if dst != src:
  73. # unless a target name was given, then treat name
  74. # as a directory and append a basename
  75. entry_name_fn = lambda name: \
  76. os.path.join(dst,
  77. os.path.basename(name))
  78. srcs = glob(os.path.join(kernel_dir, src))
  79. logger.debug('Globbed sources: %s', ', '.join(srcs))
  80. for entry in srcs:
  81. src = os.path.relpath(entry, kernel_dir)
  82. entry_dst_name = entry_name_fn(entry)
  83. cls.install_task.append((src, entry_dst_name))
  84. else:
  85. cls.install_task.append((src, dst))
  86. if source_params.get('loader') != "u-boot":
  87. return
  88. configfile = cr.ks.bootloader.configfile
  89. custom_cfg = None
  90. if configfile:
  91. custom_cfg = get_custom_config(configfile)
  92. if custom_cfg:
  93. # Use a custom configuration for extlinux.conf
  94. extlinux_conf = custom_cfg
  95. logger.debug("Using custom configuration file "
  96. "%s for extlinux.cfg", configfile)
  97. else:
  98. raise WicError("configfile is specified but failed to "
  99. "get it from %s." % configfile)
  100. if not custom_cfg:
  101. # The kernel types supported by the sysboot of u-boot
  102. kernel_types = ["zImage", "Image", "fitImage", "uImage", "vmlinux"]
  103. has_dtb = False
  104. fdt_dir = '/'
  105. kernel_name = None
  106. # Find the kernel image name, from the highest precedence to lowest
  107. for image in kernel_types:
  108. for task in cls.install_task:
  109. src, dst = task
  110. if re.match(image, src):
  111. kernel_name = os.path.join('/', dst)
  112. break
  113. if kernel_name:
  114. break
  115. for task in cls.install_task:
  116. src, dst = task
  117. # We suppose that all the dtb are in the same directory
  118. if re.search(r'\.dtb', src) and fdt_dir == '/':
  119. has_dtb = True
  120. fdt_dir = os.path.join(fdt_dir, os.path.dirname(dst))
  121. break
  122. if not kernel_name:
  123. raise WicError('No kernel file found')
  124. # Compose the extlinux.conf
  125. extlinux_conf = "default Yocto\n"
  126. extlinux_conf += "label Yocto\n"
  127. extlinux_conf += " kernel %s\n" % kernel_name
  128. if has_dtb:
  129. extlinux_conf += " fdtdir %s\n" % fdt_dir
  130. bootloader = cr.ks.bootloader
  131. extlinux_conf += "append root=%s rootwait %s\n" \
  132. % (cr.rootdev, bootloader.append if bootloader.append else '')
  133. install_cmd = "install -d %s/extlinux/" % hdddir
  134. exec_cmd(install_cmd)
  135. cfg = open("%s/extlinux/extlinux.conf" % hdddir, "w")
  136. cfg.write(extlinux_conf)
  137. cfg.close()
  138. @classmethod
  139. def do_prepare_partition(cls, part, source_params, cr, cr_workdir,
  140. oe_builddir, bootimg_dir, kernel_dir,
  141. rootfs_dir, native_sysroot):
  142. """
  143. Called to do the actual content population for a partition i.e. it
  144. 'prepares' the partition to be incorporated into the image.
  145. In this case, does the following:
  146. - sets up a vfat partition
  147. - copies all files listed in IMAGE_BOOT_FILES variable
  148. """
  149. hdddir = "%s/boot.%d" % (cr_workdir, part.lineno)
  150. if not kernel_dir:
  151. kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
  152. if not kernel_dir:
  153. raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
  154. logger.debug('Kernel dir: %s', bootimg_dir)
  155. for task in cls.install_task:
  156. src_path, dst_path = task
  157. logger.debug('Install %s as %s', src_path, dst_path)
  158. install_cmd = "install -m 0644 -D %s %s" \
  159. % (os.path.join(kernel_dir, src_path),
  160. os.path.join(hdddir, dst_path))
  161. exec_cmd(install_cmd)
  162. logger.debug('Prepare boot partition using rootfs in %s', hdddir)
  163. part.prepare_rootfs(cr_workdir, oe_builddir, hdddir,
  164. native_sysroot, False)