summaryrefslogtreecommitdiffstats
path: root/scripts/mk-images.efi
blob: 5f13edaa3b6cabb09b7564b1119e28b6776aff63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#
# mk-images.efi
#
# Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

makeefibootdisk()
{
    partimg=$1
    target=$2

    if [ ! -f $1 ]; then
        return
    fi

    local partsize=$(ls -l $1 | awk '{ print $5; }')
    local disksize=$((17408 + $partsize + 17408))
    disksize=$(($disksize + $(($disksize % 512))))
    local diskimg=$(mktemp /tmp/efidisk.img.XXXXXX)
    dd if=/dev/zero of=$diskimg count=1 bs=$disksize
    local loop=$(losetup -v -f $diskimg | awk '{ print $4 }')
    dmsetup create efiboot$$ --table "0 $(($disksize / 512)) linear $loop 0"
    parted --script /dev/mapper/efiboot$$ mklabel gpt unit b mkpart '"EFI System Partition"' fat32 17408 $((17408 + $partsize)) set 1 boot on
    dd if=$partimg of=/dev/mapper/efiboot$$p1
    dmsetup remove /dev/mapper/efiboot$$p1
    dmsetup remove /dev/mapper/efiboot$$
    losetup -d $loop

    mv -v $diskimg $target
    chmod a+r $target
}

#makeefibootimage required for EFI bootloader dosfs image
makeefibootimage() {
    MBD_FILENAME=""
    KERNELFILE=""
    INITRDFILE=""
    grubpkg=""
    MBD_TMPIMAGE=${TMPDIR:-/tmp}/makebootdisk.image.$$
    MBD_BOOTTREE=${TMPDIR:-/tmp}/makebootdisk.tree.$$
    MBD_BOOTTREE_TMP=$MBD_BOOTTREE'_tmp'
    while [ x$(echo $1 | cut -c1-2) = x"--" ]; do
    if [ $1 = "--kernel" ]; then
        KERNELFILE=$2
        shift; shift
        continue
    elif [ $1 = "--initrd" ]; then
        INITRDFILE=$2
        shift; shift
        continue
    elif [ $1 = "--imagename" ]; then
        MBD_FILENAME=$IMAGEPATH/$2
        shift; shift
        continue
    elif [ $1 = "--grubpkg" ]; then
        grubpkg=$2
        shift; shift
        continue
    fi
    echo "Unknown option passed to makebootdisk"
    exit 1
    done

    if [ -z "$MBD_FILENAME" ]; then
        echo "No imagename passed"
        exit 1
    fi

    if [ -z "$KERNELFILE" ]; then
        echo "No kernel file passed"
        exit 1
    fi

    if [ -z "$INITRDFILE" ]; then
        echo "No initrd file passed"
        exit 1
    fi
    MBD_FSIMAGE="$INITRDFILE"

    mkdir -p $MBD_BOOTTREE
    mkdir -p $MBD_BOOTTREE_TMP
    rm -rf $MBD_BOOTTREE_TMP
    mkdir -p $MBD_TMPIMAGE

    # provided by the mk-image.$ARCH file
    prepareEfiImage

    left=$(df $MBD_BOOTTREE | tail -n1)
    left=$(echo $left | awk '{print $4'})

    umount $MBD_BOOTTREE

    if [ -n "$EXTRAKERNELPATH" ]; then
        mkdir -p `dirname $EXTRAKERNELPATH`
        cp -f $KERNELROOT/$KERNELDIR/${KERNELNAME}-* $EXTRAKERNELPATH
    fi

    mkdir -p `dirname $MBD_FILENAME`
    rm -rf $MBD_TMPIMAGE $MBD_MNTPOINT $MBD_BOOTTREE
    if [ -z "$INITRDFILE" ]; then
        rm -f $MBD_FSIMAGE
    fi

    chmod a+r $MBD_FILENAME
    echo "Wrote $MBD_FILENAME (${left}k free)"
}

# prepare and build an efiboot.img.
prepareEfiImage() {
    prepareEfiTree || return 1

    # dynamically calculate the size of the dosfs
    BOOTDISKSIZE=$(du -kcs $MBD_BOOTTREE_TMP | tail -n1 | awk '{print $1}')
    BOOTDISKSIZE=$(expr $BOOTDISKSIZE + 100)
    echo "The size of the efiboot.img dosfs is $BOOTDISKSIZE"
    mkdosfs -n ANACONDA -C $MBD_FILENAME $BOOTDISKSIZE >/dev/null
    mount -o loop,shortname=winnt,umask=0077 -t vfat $MBD_FILENAME $MBD_BOOTTREE
    cp -R $MBD_BOOTTREE_TMP/* $MBD_BOOTTREE
}

# prepare a directory with the kernel, initrd, and various message files
# used to populate the efi boot image
prepareEfiTree() {
    mkdir -p $MBD_BOOTTREE_TMP/EFI/boot

    cp -a $BOOTDISKDIR/* $MBD_BOOTTREE_TMP/EFI/boot/
    cp $INITRDFILE $MBD_BOOTTREE_TMP/EFI/boot/initrd.img
    cp $KERNELFILE $MBD_BOOTTREE_TMP/EFI/boot/vmlinuz

    sed -i "s/@PRODUCT@/$PRODUCT/g" $MBD_BOOTTREE_TMP/EFI/boot/grub.conf
    sed -i "s/@VERSION@/$VERSION/g" $MBD_BOOTTREE_TMP/EFI/boot/grub.conf

    yumdownloader -c $yumconf $grubpkg
    rpm2cpio $grubpkg.rpm | (cd $KERNELROOT; cpio --quiet -iumd)
    cp $KERNELROOT/boot/efi/EFI/redhat/grub.efi $MBD_BOOTTREE_TMP/EFI/boot/grub.efi

    # The first generation Mactel machines get the bootloader name wrong
    # as per the spec.  Awesome, guys.
    if [ "$efiarch" == "ia32" ]; then
        cp $MBD_BOOTTREE_TMP/EFI/boot/grub.efi $MBD_BOOTTREE_TMP/EFI/boot/boot.efi
        cp $MBD_BOOTTREE_TMP/EFI/boot/grub.conf $MBD_BOOTTREE_TMP/EFI/boot/boot.conf
    fi

    mv $MBD_BOOTTREE_TMP/EFI/boot/grub.efi $MBD_BOOTTREE_TMP/EFI/boot/boot${efiarch}.efi
    mv $MBD_BOOTTREE_TMP/EFI/boot/grub.conf $MBD_BOOTTREE_TMP/EFI/boot/boot${efiarch}.conf

    artpkg=$(repoquery --qf "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}" --whatprovides ${brandpkgname}-logos | grep -v generic-logos | head -1)
    if [ -z "$artpkg" ]; then
       argpkg="generic-logos"
    fi

    yumdownloader -c ${yumconf} ${artpkg}
    rpm2cpio ${artpkg} | (cd $KERNELROOT; cpio --quiet -iumd)
    cp $KERNELROOT/boot/grub/splash.xpm.gz $MBD_BOOTTREE_TMP/EFI/boot/splash.xpm.gz
}

makeEfiImages() {
    yumconf="$1"
    if [ "$kernelvers" != "$kernelxen" ]; then
        local grubarch=${efiarch}
        case ${efiarch} in
            ia32) grubarch=i?86 ;;
            x64) grubarch=x86_64 ;;
        esac

        grubpkg=$(repoquery --qf "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}" -c $yumconf grub.$grubarch)

        if [ -z "$grubpkg" ]; then
            echo "cannot find package grub.$grubarch" >&2
            return 1
        fi
        echo "Building efiboot.img for ${efiarch}/$KERNELARCH at $TOPDESTPATH/images/efiboot.img"

        makeefibootimage \
            --imagename pxeboot/efiboot.img \
            --kernel $TOPDESTPATH/images/pxeboot/vmlinuz \
            --initrd $TOPDESTPATH/images/pxeboot/initrd.img \
            --grubpkg ${grubpkg}
        local ret=$?
        [ $ret -eq 0 ] || return $ret

        makeefibootdisk $TOPDESTPATH/images/pxeboot/efiboot.img $TOPDESTPATH/images/efidisk.img
        return $?
    fi
    return 1
}