1#!/bin/bash
2#
3#Header for BMC DUMP
4#This script will create header file only for IBM systems.
5#This script will generate generic IBM dump header format.
6#
7#Note: The dump header will be imposed on the dump file i.e
8#<obmdump file>.tar.xz only on IBM specific systems, user needs to
9#separate out the header before extracting the dump.
10#
11
12#Constants
13declare -rx INVENTORY_MANAGER='xyz.openbmc_project.Inventory.Manager'
14declare -rx INVENTORY_PATH='/xyz/openbmc_project/inventory/system'
15declare -rx INVENTORY_ASSET_INT='xyz.openbmc_project.Inventory.Decorator.Asset'
16declare -rx DUMP_HEADER_ENTRY_SIZE='516'
17declare -rx INVENTORY_BMC_BOARD='/xyz/openbmc_project/inventory/system/chassis/motherboard'
18declare -rx SIZE_4='4'
19declare -rx SIZE_8='8'
20declare -rx SIZE_12='12'
21declare -rx SIZE_32='32'
22
23#Variables
24declare -x FILE="/tmp/dumpheader_$EPOCHTIME"
25declare -x dumpSize=$(ls -al $name_dir.tar.xz | awk '{print $5}')
26declare -x modelNo=$(busctl get-property $INVENTORY_MANAGER $INVENTORY_PATH \
27$INVENTORY_ASSET_INT Model | cut -d " " -f 2 | sed "s/^\(\"\)\(.*\)\1\$/\2/g")
28
29declare -x serialNo=$(busctl get-property $INVENTORY_MANAGER $INVENTORY_PATH \
30$INVENTORY_ASSET_INT SerialNumber | cut -d " " -f 2 | sed "s/^\(\"\)\(.*\)\1\$/\2/g")
31
32declare -x dDay=$(date -d @$EPOCHTIME +'%Y%m%d%H%M%S')
33
34declare -x bmcSerialNo=$(busctl call $INVENTORY_MANAGER $INVENTORY_BMC_BOARD \
35org.freedesktop.DBus.Properties Get ss $INVENTORY_ASSET_INT \
36SerialNumber | cut -d " " -f 3 | sed "s/^\(\"\)\(.*\)\1\$/\2/g")
37
38#Function to add NULL
39function add_null () {
40    local a=$1
41    printf '%*s' $a | tr ' ' "\0" >> $FILE
42}
43
44#Function to is to convert the EPOCHTIME collected
45#from dreport into hex values and write the same in
46#header.
47function dump_time () {
48    x=${#dDay}
49    msize=`expr $x / 2`
50    msize=`expr $SIZE_8 - $msize`
51    for ((i=0;i<$x;i+=2));
52    do
53        printf \\x${dDay:$i:2} >> $FILE
54    done
55    add_null $msize
56}
57
58#Function to fetch the size of the dump
59function dump_size () {
60    #Adding 516 bytes as the total dump size is dump tar size
61    #plus the dump header entry in this case
62    #dump_header and dump_entry
63    sizeDump=`expr $dumpSize + $DUMP_HEADER_ENTRY_SIZE`
64    printf -v hex "%x" $sizeDump
65    x=${#hex}
66    if [ $(($x % 2)) -eq 1 ]; then
67        hex=0$hex
68        x=${#hex}
69    fi
70    msize=`expr $x / 2`
71    msize=`expr $SIZE_8 - $msize`
72    add_null $msize
73    for ((i=0;i<$x;i+=2));
74    do
75        printf \\x${hex:$i:2} >> $FILE
76    done
77}
78
79#Function to set dump id to 8 bytes format
80function get_dump_id () {
81    x=${#dump_id}
82    nulltoadd=`expr $SIZE_8 - $x`
83    printf '%*s' $nulltoadd | tr ' ' "0" >> $FILE
84    printf $dump_id >> $FILE
85}
86
87#Function to get the bmc serial number
88function getbmc_serial () {
89    x=${#bmcSerialNo}
90    nulltoadd=`expr $SIZE_12 - $x`
91    printf $bmcSerialNo >> $FILE
92    printf '%*s' $nulltoadd | tr ' ' "0" >> $FILE
93}
94
95#Function to add virtual file directory entry, consists of below entries
96####################FORMAT################
97#Name              Size(bytes)  Value
98#Entry Header      8            FILE
99#Entry Size        2            0x0040
100#Reserved          10           NULL
101#Entry Type        2            0x0001
102#File Name Prefix  2            0x000F
103#Dump File Type    7            BMCDUMP
104#Separator         1            .
105#System Serial No  7            System serial number fetched from system
106#Dump Identifier   8            Dump Identifier value fetched from dump
107#Separator         1            .
108#Time stamp        14           Form should be yyyymmddhhmmss
109#Null Terminator   1            0x00
110function dump_file_entry () {
111    printf "FILE    " >> $FILE
112    add_null 1
113    printf '\x40' >> $FILE #Virtual file directory entry size
114    add_null 11
115    printf '\x01' >> $FILE
116    add_null 1
117    printf '\x0F' >> $FILE
118    printf "BMPDUMP.%s." "$serialNo" >> $FILE
119    get_dump_id
120    printf "." >> $FILE
121    printf $dDay >> $FILE  #UTC time stamp
122    add_null 1
123}
124
125#Function section directory entry, consists of below entries
126####################FORMAT################
127#Name              Size(bytes)  Value
128#Entry Header      8            SECTION
129#Entry Size        2            0x0030
130#Section Priority  2            0x0000
131#Reserved          4            NULL
132#Entry Flags       4            0x00000001
133#Entry Types       2            0x0002
134#Reserved          2            NULL
135#Dump Size         8            Dump size in hex + dump header
136#Optional Section  16           BMCDUMP
137function dump_section_entry () {
138    printf "SECTION " >> $FILE
139    add_null 1
140    printf '\x30' >> $FILE #Section entry size
141    add_null 9
142    printf '\x01' >> $FILE
143    add_null 1
144    printf '\x02' >> $FILE
145    add_null 2
146    dump_size    #Dump size
147    printf "BMCDUMP" >> $FILE
148    add_null 9
149}
150
151#Function to add dump header, consists of below entries
152####################FORMAT################
153#Name              Size(bytes)  Value
154#Dump type         8            BMC DUMP
155#Dump Request time 8            Dump request time stamp (in BCD)
156#Dump Identifier   4            Dump identifer fetched from dump
157#Dump version      2            0x0210
158#Dump header       2            0x200
159#Total dump size   8            Dump size + dump header
160#Panel function    32           System model, feature, type and IPL mode
161#System Name       32           System Name (in ASCII)
162#Serial number     7            System serial number
163#Reserved          1            NULL
164#PLID              4            Comes from errorlog
165#File Header Size  2            0x70
166#Dump SRC Size     2            Dump SRC Size. Currently NULL
167#DUMP SRC          320          DUMP SRC. Currently NULL
168#Dump Req Type     4            Dump requester user interface type.
169#Dump Req ID       32           Dump requester user interface ID
170#Dump Req user ID  32           Dump requester user ID.
171#
172#TODO: Github issue #2639, to populate the unpopulated elements.
173#Note: Unpopulated elements are listed below are set as NULL
174#PLID
175#SRC size
176#SRC dump
177#Dump requestor type
178#Dump Req ID
179#Dump Req user ID
180function dump_header () {
181    printf "BMC DUMP" >> $FILE
182    dump_time
183    add_null 4 #Dump Identifier
184    printf '\x02' >> $FILE #Dump version 0x0210
185    printf '\x10' >> $FILE
186    printf '\x02' >> $FILE #Dump header size 0x0200
187    add_null 1
188    dump_size  #dump size
189    printf $modelNo >> $FILE
190    add_null 24
191    printf "Server-%s-SN-%s" "$modelNo" "$serialNo" >> $FILE
192    add_null 7
193    printf $serialNo >> $FILE
194    add_null 1
195    add_null 4 #PLID
196    printf '\x70' >> $FILE #File header size
197    add_null 2 # SRC size
198    add_null 320 # SRC dump
199    getbmc_serial
200    add_null 68 # Dump requester details
201}
202
203#Function to add Dump entry, consists of below entries
204####################FORMAT################
205#Name               Size(bytes)  Value
206#Dump Entry Version 1            0x01
207#BMC Dump Valid     1            0x01
208#No of Dump Entry   2            Number of Dump Entry
209#
210function dump_entry () {
211    printf '\x01' >> $FILE #Dump entry version
212    printf '\x01' >> $FILE #Dump valid
213    add_null 1
214    printf '\x10' >> $FILE #Dump entry
215}
216
217#main function
218function gen_header_package () {
219    dump_file_entry
220    dump_section_entry
221    dump_header
222    dump_entry
223}
224
225#Run gen_header_package
226gen_header_package
227