1#!/bin/bash
2#-------------------------------------------------------------------------------
3# Copyright (c) 2023, Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#-------------------------------------------------------------------------------
8
9######################################################################
10# This script is to create a flash gpt image for corstone platform
11#
12#  Flash image layout:
13#       |------------------------------|
14#       |        Protective MBR        |
15#       |------------------------------|
16#       |          GPT Header          |
17#       |------------------------------|
18#       |          reserved_1          |
19#       |------------------------------|
20#       |         FWU-Metadata         |
21#       |------------------------------|
22#       |      Bkup-FWU-Metadata       |
23#       |------------------------------|
24#       |  private_metadata_replica_1  |
25#       |------------------------------|
26#       |  private_metadata_replica_2  |
27#       |------------------------------|
28#       |         bl2_primary          |
29#       |------------------------------|
30#       |         tfm_primary          |
31#       |------------------------------|
32#       |                              |
33#       |                              |
34#       |------------------------------|
35#       |        bl2_secondary         |
36#       |------------------------------|
37#       |        tfm_secondary         |
38#       |------------------------------|
39#       |                              |
40#       |                              |
41#       |------------------------------|
42#       |         reserved_2           |
43#       |------------------------------|
44#       |       BKUP GPT Header        |
45#       |------------------------------|
46######################################################################
47
48
49# Path to build directory that contains binaries must be provided
50if [ -z "$1" ]; then
51  echo "Error: Build directory path argument must be provided."
52  echo "Usage: $0 [build directory path] [output image name (default is cs1000.bin)]"
53  exit 1
54fi
55
56# Set build directory
57BIN_DIR=$1
58# Set output image
59IMAGE=$BIN_DIR/${2:-cs1000.bin}
60# Set the image  size
61IMAGE_SIZE=32M
62
63# Set Type UUIDs
64FWU_METADATA_TYPE_UUID="8A7A84A0-8387-40F6-AB41-A8B9A5A60D23"
65PRIVATE_METADATA_TYPE_UUID="ECB55DC3-8AB7-4A84-AB56-EB0A9974DB42"
66SE_BL2_TYPE_UUID="64BD8ADB-02C0-4819-8688-03AB4CAB0ED9"
67TFM_TYPE_UUID="D763C27F-07F6-4FF0-B2F3-060CB465CD4E"
68
69# Create the image
70rm -f $IMAGE
71echo "remove old image if exists"
72(dd if=/dev/zero of=$IMAGE bs=$IMAGE_SIZE count=1 && echo "Image has been created" ) ||\
73(echo "something goes wrong while creating the image" && exit 1)
74
75# Create the image using sgdisk
76sgdisk  --mbrtogpt \
77        --clear \
78        --set-alignment 1 \
79        --new=1:34:+3k --partition-guid=1:$(uuidgen) --change-name=1:'reserved_1' \
80        --new=2:40:+4K --typecode=2:$FWU_METADATA_TYPE_UUID --partition-guid=2:$(uuidgen) --change-name=2:'FWU-Metadata' \
81        --new=3:48:+4K --typecode=3:$FWU_METADATA_TYPE_UUID --partition-guid=3:$(uuidgen) --change-name=3:'Bkup-FWU-Metadata' \
82        --new=4:56:+4K --typecode=4:$PRIVATE_METADATA_TYPE_UUID --partition-guid=4:$(uuidgen) --change-name=4:'private_metadata_replica_1' \
83        --new=5:64:+4k --typecode=5:$PRIVATE_METADATA_TYPE_UUID --partition-guid=5:$(uuidgen) --change-name=5:'private_metadata_replica_2' \
84        --new=6:72:+100k --typecode=6:$SE_BL2_TYPE_UUID --partition-guid=6:$(uuidgen) --change-name=6:'bl2_primary' \
85        --new=7:272:+368K --typecode=7:$TFM_TYPE_UUID --partition-guid=7:$(uuidgen) --change-name=7:'tfm_primary' \
86        --new=8:32784:+100k --typecode=8:$SE_BL2_TYPE_UUID --partition-guid=8:$(uuidgen) --change-name=8:'bl2_secondary' \
87        --new=9:32984:+368K --typecode=9:$TFM_TYPE_UUID --partition-guid=9:$(uuidgen) --change-name=9:'tfm_secondary' \
88        --new=10:65496:65501  --partition-guid=10:$(uuidgen) --change-name=10:'reserved_2' \
89        $IMAGE
90
91[ $? -ne 0 ] && echo "Error occurs while writing the GPT layout" && exit 1
92
93# Write partitions
94# conv=notrunc avoids truncation to keep the geometry of the image.
95dd if=$BIN_DIR/bl2_signed.bin of=${IMAGE}  seek=72 conv=notrunc
96dd if=$BIN_DIR/tfm_s_signed.bin of=${IMAGE} seek=272 conv=notrunc
97
98# Print the gpt table
99sgdisk -p $IMAGE
100