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