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