Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # Copyright (C) 2004, Advanced Micro Devices, Inc. |
| 2 | # |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | |
| 5 | # Creates a bootable image using syslinux, your kernel and an optional |
| 6 | # initrd |
| 7 | |
| 8 | # |
| 9 | # End result is two things: |
| 10 | # |
| 11 | # 1. A .hddimg file which is an msdos filesystem containing syslinux, a kernel, |
| 12 | # an initrd and a rootfs image. These can be written to harddisks directly and |
| 13 | # also booted on USB flash disks (write them there with dd). |
| 14 | # |
| 15 | # 2. A CD .iso image |
| 16 | |
| 17 | # Boot process is that the initrd will boot and process which label was selected |
| 18 | # in syslinux. Actions based on the label are then performed (e.g. installing to |
| 19 | # an hdd) |
| 20 | |
| 21 | # External variables (also used by syslinux.bbclass) |
| 22 | # ${INITRD} - indicates a list of filesystem images to concatenate and use as an initrd (optional) |
| 23 | # ${HDDIMG_ID} - FAT image volume-id |
| 24 | # ${ROOTFS} - indicates a filesystem image to include as the root filesystem (optional) |
| 25 | |
| 26 | inherit live-vm-common image-artifact-names |
| 27 | |
| 28 | do_bootimg[depends] += "dosfstools-native:do_populate_sysroot \ |
| 29 | mtools-native:do_populate_sysroot \ |
| 30 | cdrtools-native:do_populate_sysroot \ |
| 31 | virtual/kernel:do_deploy \ |
| 32 | ${MLPREFIX}syslinux:do_populate_sysroot \ |
| 33 | syslinux-native:do_populate_sysroot \ |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame] | 34 | ${@'%s:do_image_%s' % (d.getVar('PN'), d.getVar('LIVE_ROOTFS_TYPE').replace('-', '_').split('.')[0]) if d.getVar('ROOTFS') else ''} \ |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 35 | " |
| 36 | |
| 37 | |
| 38 | LABELS_LIVE ?= "boot install" |
| 39 | ROOT_LIVE ?= "root=/dev/ram0" |
| 40 | INITRD_IMAGE_LIVE ?= "${MLPREFIX}core-image-minimal-initramfs" |
Patrick Williams | e760df8 | 2023-05-26 11:10:49 -0500 | [diff] [blame] | 41 | INITRD_LIVE ?= "${DEPLOY_DIR_IMAGE}/${INITRD_IMAGE_LIVE}${IMAGE_MACHINE_SUFFIX}.${INITRAMFS_FSTYPES}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 42 | |
| 43 | LIVE_ROOTFS_TYPE ?= "ext4" |
| 44 | ROOTFS ?= "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.${LIVE_ROOTFS_TYPE}" |
| 45 | |
| 46 | IMAGE_TYPEDEP:live = "${LIVE_ROOTFS_TYPE}" |
| 47 | IMAGE_TYPEDEP:iso = "${LIVE_ROOTFS_TYPE}" |
| 48 | IMAGE_TYPEDEP:hddimg = "${LIVE_ROOTFS_TYPE}" |
| 49 | IMAGE_TYPES_MASKED += "live hddimg iso" |
| 50 | |
| 51 | python() { |
| 52 | image_b = d.getVar('IMAGE_BASENAME') |
| 53 | initrd_i = d.getVar('INITRD_IMAGE_LIVE') |
| 54 | if image_b == initrd_i: |
| 55 | bb.error('INITRD_IMAGE_LIVE %s cannot use image live, hddimg or iso.' % initrd_i) |
| 56 | bb.fatal('Check IMAGE_FSTYPES and INITRAMFS_FSTYPES settings.') |
| 57 | elif initrd_i: |
| 58 | d.appendVarFlag('do_bootimg', 'depends', ' %s:do_image_complete' % initrd_i) |
| 59 | } |
| 60 | |
| 61 | HDDDIR = "${S}/hddimg" |
| 62 | ISODIR = "${S}/iso" |
| 63 | EFIIMGDIR = "${S}/efi_img" |
| 64 | COMPACT_ISODIR = "${S}/iso.z" |
| 65 | |
| 66 | ISOLINUXDIR ?= "/isolinux" |
| 67 | ISO_BOOTIMG = "isolinux/isolinux.bin" |
| 68 | ISO_BOOTCAT = "isolinux/boot.cat" |
| 69 | MKISOFS_OPTIONS = "-no-emul-boot -boot-load-size 4 -boot-info-table" |
| 70 | |
| 71 | BOOTIMG_VOLUME_ID ?= "boot" |
| 72 | BOOTIMG_EXTRA_SPACE ?= "512" |
| 73 | |
| 74 | populate_live() { |
| 75 | populate_kernel $1 |
| 76 | if [ -s "${ROOTFS}" ]; then |
| 77 | install -m 0644 ${ROOTFS} $1/rootfs.img |
| 78 | fi |
| 79 | } |
| 80 | |
| 81 | build_iso() { |
| 82 | # Only create an ISO if we have an INITRD and the live or iso image type was selected |
| 83 | if [ -z "${INITRD}" ] || [ "${@bb.utils.contains_any('IMAGE_FSTYPES', 'live iso', '1', '0', d)}" != "1" ]; then |
| 84 | bbnote "ISO image will not be created." |
| 85 | return |
| 86 | fi |
| 87 | # ${INITRD} is a list of multiple filesystem images |
| 88 | for fs in ${INITRD} |
| 89 | do |
| 90 | if [ ! -s "$fs" ]; then |
| 91 | bbwarn "ISO image will not be created. $fs is invalid." |
| 92 | return |
| 93 | fi |
| 94 | done |
| 95 | |
| 96 | populate_live ${ISODIR} |
| 97 | |
| 98 | if [ "${PCBIOS}" = "1" ]; then |
| 99 | syslinux_iso_populate ${ISODIR} |
| 100 | fi |
| 101 | if [ "${EFI}" = "1" ]; then |
| 102 | efi_iso_populate ${ISODIR} |
| 103 | build_fat_img ${EFIIMGDIR} ${ISODIR}/efi.img |
| 104 | fi |
| 105 | |
| 106 | # EFI only |
| 107 | if [ "${PCBIOS}" != "1" ] && [ "${EFI}" = "1" ] ; then |
| 108 | # Work around bug in isohybrid where it requires isolinux.bin |
| 109 | # In the boot catalog, even though it is not used |
| 110 | mkdir -p ${ISODIR}/${ISOLINUXDIR} |
| 111 | install -m 0644 ${STAGING_DATADIR}/syslinux/isolinux.bin ${ISODIR}${ISOLINUXDIR} |
| 112 | fi |
| 113 | |
| 114 | # We used to have support for zisofs; this is a relic of that |
| 115 | mkisofs_compress_opts="-r" |
| 116 | |
| 117 | # Check the size of ${ISODIR}/rootfs.img, use mkisofs -iso-level 3 |
| 118 | # when it exceeds 3.8GB, the specification is 4G - 1 bytes, we need |
| 119 | # leave a few space for other files. |
| 120 | mkisofs_iso_level="" |
| 121 | |
| 122 | if [ -n "${ROOTFS}" ] && [ -s "${ROOTFS}" ]; then |
| 123 | rootfs_img_size=`stat -c '%s' ${ISODIR}/rootfs.img` |
| 124 | # 4080218931 = 3.8 * 1024 * 1024 * 1024 |
| 125 | if [ $rootfs_img_size -gt 4080218931 ]; then |
| 126 | bbnote "${ISODIR}/rootfs.img execeeds 3.8GB, using '-iso-level 3' for mkisofs" |
| 127 | mkisofs_iso_level="-iso-level 3" |
| 128 | fi |
| 129 | fi |
| 130 | |
| 131 | if [ "${PCBIOS}" = "1" ] && [ "${EFI}" != "1" ] ; then |
| 132 | # PCBIOS only media |
| 133 | mkisofs -V ${BOOTIMG_VOLUME_ID} \ |
| 134 | -o ${IMGDEPLOYDIR}/${IMAGE_NAME}.iso \ |
| 135 | -b ${ISO_BOOTIMG} -c ${ISO_BOOTCAT} \ |
| 136 | $mkisofs_compress_opts \ |
| 137 | ${MKISOFS_OPTIONS} $mkisofs_iso_level ${ISODIR} |
| 138 | else |
| 139 | # EFI only OR EFI+PCBIOS |
| 140 | mkisofs -A ${BOOTIMG_VOLUME_ID} -V ${BOOTIMG_VOLUME_ID} \ |
| 141 | -o ${IMGDEPLOYDIR}/${IMAGE_NAME}.iso \ |
| 142 | -b ${ISO_BOOTIMG} -c ${ISO_BOOTCAT} \ |
| 143 | $mkisofs_compress_opts ${MKISOFS_OPTIONS} $mkisofs_iso_level \ |
| 144 | -eltorito-alt-boot -eltorito-platform efi \ |
| 145 | -b efi.img -no-emul-boot \ |
| 146 | ${ISODIR} |
| 147 | isohybrid_args="-u" |
| 148 | fi |
| 149 | |
| 150 | isohybrid $isohybrid_args ${IMGDEPLOYDIR}/${IMAGE_NAME}.iso |
| 151 | } |
| 152 | |
| 153 | build_fat_img() { |
| 154 | FATSOURCEDIR=$1 |
| 155 | FATIMG=$2 |
| 156 | |
| 157 | # Calculate the size required for the final image including the |
| 158 | # data and filesystem overhead. |
| 159 | # Sectors: 512 bytes |
| 160 | # Blocks: 1024 bytes |
| 161 | |
| 162 | # Determine the sector count just for the data |
| 163 | SECTORS=$(expr $(du --apparent-size -ks ${FATSOURCEDIR} | cut -f 1) \* 2) |
| 164 | |
| 165 | # Account for the filesystem overhead. This includes directory |
| 166 | # entries in the clusters as well as the FAT itself. |
| 167 | # Assumptions: |
| 168 | # FAT32 (12 or 16 may be selected by mkdosfs, but the extra |
| 169 | # padding will be minimal on those smaller images and not |
| 170 | # worth the logic here to caclulate the smaller FAT sizes) |
| 171 | # < 16 entries per directory |
| 172 | # 8.3 filenames only |
| 173 | |
| 174 | # 32 bytes per dir entry |
| 175 | DIR_BYTES=$(expr $(find ${FATSOURCEDIR} | tail -n +2 | wc -l) \* 32) |
| 176 | # 32 bytes for every end-of-directory dir entry |
| 177 | DIR_BYTES=$(expr $DIR_BYTES + $(expr $(find ${FATSOURCEDIR} -type d | tail -n +2 | wc -l) \* 32)) |
| 178 | # 4 bytes per FAT entry per sector of data |
| 179 | FAT_BYTES=$(expr $SECTORS \* 4) |
| 180 | # 4 bytes per FAT entry per end-of-cluster list |
| 181 | FAT_BYTES=$(expr $FAT_BYTES + $(expr $(find ${FATSOURCEDIR} -type d | tail -n +2 | wc -l) \* 4)) |
| 182 | |
| 183 | # Use a ceiling function to determine FS overhead in sectors |
| 184 | DIR_SECTORS=$(expr $(expr $DIR_BYTES + 511) / 512) |
| 185 | # There are two FATs on the image |
| 186 | FAT_SECTORS=$(expr $(expr $(expr $FAT_BYTES + 511) / 512) \* 2) |
| 187 | SECTORS=$(expr $SECTORS + $(expr $DIR_SECTORS + $FAT_SECTORS)) |
| 188 | |
| 189 | # Determine the final size in blocks accounting for some padding |
| 190 | BLOCKS=$(expr $(expr $SECTORS / 2) + ${BOOTIMG_EXTRA_SPACE}) |
| 191 | |
| 192 | # mkdosfs will sometimes use FAT16 when it is not appropriate, |
| 193 | # resulting in a boot failure from SYSLINUX. Use FAT32 for |
| 194 | # images larger than 512MB, otherwise let mkdosfs decide. |
| 195 | if [ $(expr $BLOCKS / 1024) -gt 512 ]; then |
| 196 | FATSIZE="-F 32" |
| 197 | fi |
| 198 | |
| 199 | # mkdosfs will fail if ${FATIMG} exists. Since we are creating an |
| 200 | # new image, it is safe to delete any previous image. |
| 201 | if [ -e ${FATIMG} ]; then |
| 202 | rm ${FATIMG} |
| 203 | fi |
| 204 | |
| 205 | if [ -z "${HDDIMG_ID}" ]; then |
| 206 | mkdosfs ${FATSIZE} -n ${BOOTIMG_VOLUME_ID} ${MKDOSFS_EXTRAOPTS} -C ${FATIMG} \ |
| 207 | ${BLOCKS} |
| 208 | else |
| 209 | mkdosfs ${FATSIZE} -n ${BOOTIMG_VOLUME_ID} ${MKDOSFS_EXTRAOPTS} -C ${FATIMG} \ |
| 210 | ${BLOCKS} -i ${HDDIMG_ID} |
| 211 | fi |
| 212 | |
| 213 | # Copy FATSOURCEDIR recursively into the image file directly |
| 214 | mcopy -i ${FATIMG} -s ${FATSOURCEDIR}/* ::/ |
| 215 | } |
| 216 | |
| 217 | build_hddimg() { |
| 218 | # Create an HDD image |
| 219 | if [ "${@bb.utils.contains_any('IMAGE_FSTYPES', 'live hddimg', '1', '0', d)}" = "1" ] ; then |
| 220 | populate_live ${HDDDIR} |
| 221 | |
| 222 | if [ "${PCBIOS}" = "1" ]; then |
| 223 | syslinux_hddimg_populate ${HDDDIR} |
| 224 | fi |
| 225 | if [ "${EFI}" = "1" ]; then |
| 226 | efi_hddimg_populate ${HDDDIR} |
| 227 | fi |
| 228 | |
| 229 | # Check the size of ${HDDDIR}/rootfs.img, error out if it |
| 230 | # exceeds 4GB, it is the single file's max size of FAT fs. |
| 231 | if [ -f ${HDDDIR}/rootfs.img ]; then |
| 232 | rootfs_img_size=`stat -c '%s' ${HDDDIR}/rootfs.img` |
| 233 | max_size=`expr 4 \* 1024 \* 1024 \* 1024` |
| 234 | if [ $rootfs_img_size -ge $max_size ]; then |
| 235 | bberror "${HDDDIR}/rootfs.img rootfs size is greather than or equal to 4GB," |
| 236 | bberror "and this doesn't work on a FAT filesystem. You can either:" |
| 237 | bberror "1) Reduce the size of rootfs.img, or," |
| 238 | bbfatal "2) Use wic, vmdk,vhd, vhdx or vdi instead of hddimg\n" |
| 239 | fi |
| 240 | fi |
| 241 | |
| 242 | build_fat_img ${HDDDIR} ${IMGDEPLOYDIR}/${IMAGE_NAME}.hddimg |
| 243 | |
| 244 | if [ "${PCBIOS}" = "1" ]; then |
| 245 | syslinux_hddimg_install |
| 246 | fi |
| 247 | |
| 248 | chmod 644 ${IMGDEPLOYDIR}/${IMAGE_NAME}.hddimg |
| 249 | fi |
| 250 | } |
| 251 | |
| 252 | python do_bootimg() { |
| 253 | set_live_vm_vars(d, 'LIVE') |
| 254 | if d.getVar("PCBIOS") == "1": |
| 255 | bb.build.exec_func('build_syslinux_cfg', d) |
| 256 | if d.getVar("EFI") == "1": |
| 257 | bb.build.exec_func('build_efi_cfg', d) |
| 258 | bb.build.exec_func('build_hddimg', d) |
| 259 | bb.build.exec_func('build_iso', d) |
| 260 | bb.build.exec_func('create_symlinks', d) |
| 261 | } |
| 262 | do_bootimg[subimages] = "hddimg iso" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 263 | |
| 264 | addtask bootimg before do_image_complete after do_rootfs |