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