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