aboutsummaryrefslogtreecommitdiffstats
path: root/meta-arm-bsp/classes/image_types_disk_img.bbclass
blob: 78cc0d850b148a723574fe5fc510567fbe0f5de8 (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
# Defines the disk.img image type

#
# Add an image type 'disk.img' which creates a disk image
# with up to 4 partitions
#
# For partition 1 (replace 1 by 2 for partition 2, and so on for 3 and 4):
#
# * DISK_IMG_PARTITION1_SIZE is the partition size in MB (1024 is 1 GB)
#
# * DISK_IMG_PARTITION1_FSTYPE is the file system to format the partition with.
#   We support only ext files systems (ext2, ext3 and ext4)
#   If this is empty, the partition will not be formated.
#
# * DISK_IMG_PARTITION1_CONTENT is the content to put in the filesystem.
#   Only 'rootfs' is supported and will create a partition with the Yocto
#   root filesystem.
#

# Default values for partition 1
DISK_IMG_PARTITION1_SIZE ??= "2048"
DISK_IMG_PARTITION1_FSTYPE ??= "ext4"
DISK_IMG_PARTITION1_CONTENT ??= "rootfs"

# Default values for partition 2
DISK_IMG_PARTITION2_SIZE ??= "0"
DISK_IMG_PARTITION2_FSTYPE ??= "ext2"
DISK_IMG_PARTITION2_CONTENT ??= ""

# Default values for partition 3
DISK_IMG_PARTITION3_SIZE ??= "0"
DISK_IMG_PARTITION3_FSTYPE ??= "ext4"
DISK_IMG_PARTITION3_CONTENT ??= ""

# Default values for partition 4
DISK_IMG_PARTITION4_SIZE ??= "0"
DISK_IMG_PARTITION4_FSTYPE ??= "ext4"
DISK_IMG_PARTITION4_CONTENT ??= ""

# Default disk sector size
DISK_IMG_SECTOR_SIZE ??= "512"

# We need mkfs.ext and parted tools to create our image (dd is always there)
do_image_disk_img[depends] += "e2fsprogs-native:do_populate_sysroot \
    parted-native:do_populate_sysroot"

DISK_IMG_FILE = "${IMGDEPLOYDIR}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.disk.img"

# Create one disk partition
disk_img_createpart() {
    local imagefile="$1"
    local start="$2"
    local size="$3"
    local fstype="${4:-}"
    local content="${5:-}"
    local formatargs=""

    set -x

    rm -f $imagefile

    # Create the partition image
    dd if=/dev/zero of=$imagefile bs=${DISK_IMG_SECTOR_SIZE} count=0 \
        seek=$(expr $size / ${DISK_IMG_SECTOR_SIZE})

    if [ -n "$fstype" ]; then
        case $content in
            rootfs)
                formatargs=" -d ${IMAGE_ROOTFS}"
                ;;
            boot)
                echo "Unsupported"
                exit 1
                ;;
            *)
        esac

        # Create the file system (with content if needed)
        mkfs.$fstype -F $imagefile $formatargs
    fi

    cat $imagefile >> ${DISK_IMG_FILE}

    # Add the partition to the partition table
    parted -s ${DISK_IMG_FILE} unit B mkpart primary $start \
        $(expr $start + $realsize - 1)
}

disk_img_create () {
    local currpos
    local realsize

    set -x

    currpos=${DISK_IMG_SECTOR_SIZE}

    # Create reserved part for partition table (1MB)
    dd if=/dev/zero of=${DISK_IMG_FILE} bs=${DISK_IMG_SECTOR_SIZE} count=0 \
        seek=1

    parted -s ${DISK_IMG_FILE} mklabel msdos

    if [ ${DISK_IMG_PARTITION1_SIZE} -ne 0 ]; then

        # Reduce the first block size of one sector to make space
        # for the partition table
        realsize=$(expr ${DISK_IMG_PARTITION1_SIZE} \* 1024 \* 1024 \
            - ${DISK_IMG_SECTOR_SIZE})

        # Create the partition
        disk_img_createpart ${WORKDIR}/part1.img $currpos $realsize \
            "${DISK_IMG_PARTITION1_FSTYPE}" "${DISK_IMG_PARTITION1_CONTENT}"

        currpos=$(expr $currpos + $realsize)
    fi

    if [ ${DISK_IMG_PARTITION2_SIZE} -ne 0 ]; then
        # Partition size
        realsize=$(expr ${DISK_IMG_PARTITION2_SIZE} \* 1024 \* 1024)

        # Create the partition
        disk_img_createpart ${WORKDIR}/part2.img $currpos $realsize \
            "${DISK_IMG_PARTITION2_FSTYPE}" "${DISK_IMG_PARTITION2_CONTENT}"

        currpos=$(expr $currpos + $realsize)

    fi

    if [ ${DISK_IMG_PARTITION3_SIZE} -ne 0 ]; then
        # Partition size
        realsize=$(expr ${DISK_IMG_PARTITION3_SIZE} \* 1024 \* 1024)

        # Create the partition
        disk_img_createpart ${WORKDIR}/part3.img $currpos $realsize \
            "${DISK_IMG_PARTITION3_FSTYPE}" "${DISK_IMG_PARTITION3_CONTENT}"

        currpos=$(expr $currpos + $realsize)

    fi
    if [ ${DISK_IMG_PARTITION4_SIZE} -ne 0 ]; then
        # Partition size
        realsize=$(expr ${DISK_IMG_PARTITION4_SIZE} \* 1024 \* 1024)

        # Create the partition
        disk_img_createpart ${WORKDIR}/part4.img $currpos $realsize \
            "${DISK_IMG_PARTITION4_FSTYPE}" "${DISK_IMG_PARTITION4_CONTENT}"

        currpos=$(expr $currpos + $realsize)

    fi
}

IMAGE_CMD_disk.img = "disk_img_create"
IMAGE_TYPES += "disk.img"