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#<dump name>.tar.xz/gz only on IBM specific systems, user needs to 9#separate out the header before extracting the dump. 10# 11 12#Constants 13declare -rx DUMP_HEADER_ENTRY_SIZE='516' 14declare -rx SIZE_4='4' 15declare -rx SIZE_8='8' 16declare -rx SIZE_12='12' 17declare -rx SIZE_32='32' 18#Dump Summary size without header 19declare -rx DUMP_SUMMARY_SIZE='1024' 20declare -rx HW_DUMP='00' 21declare -rx HB_DUMP='20' 22declare -rx SBE_DUMP='30' 23declare -rx MSBE_DUMP='40' 24declare -rx OP_DUMP="opdump" 25declare -rx INVENTORY_MANAGER='xyz.openbmc_project.Inventory.Manager' 26declare -rx INVENTORY_PATH='/xyz/openbmc_project/inventory/system' 27declare -rx INVENTORY_ASSET_INT='xyz.openbmc_project.Inventory.Decorator.Asset' 28declare -rx INVENTORY_BMC_BOARD='/xyz/openbmc_project/inventory/system/chassis/motherboard' 29declare -rx PHOSPHOR_LOGGING='xyz.openbmc_project.Logging' 30declare -rx PEL_ENTRY='org.open_power.Logging.PEL.Entry' 31declare -rx PEL_ID_PROP='PlatformLogID' 32 33#Variables 34declare -x modelNo 35modelNo=$(busctl get-property $INVENTORY_MANAGER $INVENTORY_PATH \ 36 $INVENTORY_ASSET_INT Model | cut -d " " -f 2 | sed "s/^\(\"\)\(.*\)\1\$/\2/g") 37 38#Variables 39declare -x serialNo="0000000" 40 41declare -x dDay 42dDay=$(date -d @"$EPOCHTIME" +'%Y%m%d%H%M%S') 43declare -x bmcSerialNo 44bmcSerialNo=$(busctl call $INVENTORY_MANAGER $INVENTORY_BMC_BOARD \ 45 org.freedesktop.DBus.Properties Get ss $INVENTORY_ASSET_INT \ 46 SerialNumber | cut -d " " -f 3 | sed "s/^\(\"\)\(.*\)\1\$/\2/g") 47 48#Source common functions 49. $DREPORT_INCLUDE/opfunctions 50 51#Function to add NULL 52function add_null() { 53 local a=$1 54 printf '%*s' $a | tr ' ' "\0" >> $FILE 55} 56 57# Function to add Originator details to dump header 58function add_originator_details() { 59 if [ -z "$ORIGINATOR_TYPE" ]; then 60 add_null 4 61 else 62 len=${#ORIGINATOR_TYPE} 63 nulltoadd=$(( SIZE_4 - len )) 64 printf '%s' "$ORIGINATOR_TYPE" >> "$FILE" 65 if [ "$nulltoadd" -gt 0 ]; then 66 add_null "$nulltoadd" 67 fi 68 fi 69 70 if [ -z "$ORIGINATOR_ID" ]; then 71 add_null 32 72 else 73 len=${#ORIGINATOR_ID} 74 nulltoadd=$(( SIZE_32 - len )) 75 printf '%s' "$ORIGINATOR_ID" >> "$FILE" 76 if [ "$nulltoadd" -gt 0 ]; then 77 add_null "$nulltoadd" 78 fi 79 fi 80} 81 82#Function to is to convert the EPOCHTIME collected 83#from dreport into hex values and write the same in 84#header. 85function dump_time() { 86 x=${#dDay} 87 msize=`expr $x / 2` 88 msize=`expr $SIZE_8 - $msize` 89 for ((i=0;i<$x;i+=2)); 90 do 91 printf \\x${dDay:$i:2} >> $FILE 92 done 93 add_null $msize 94} 95 96#Function to fetch the size of the dump 97function dump_size() { 98 #Adding 516 bytes as the total dump size is dump tar size 99 #plus the dump header entry in this case 100 #dump_header and dump_entry 101 # shellcheck disable=SC2154 # name_dir comes from elsewhere. 102 dumpSize=$(stat -c %s "$name_dir.bin") 103 sizeDump=$(( dumpSize + DUMP_HEADER_ENTRY_SIZE )) 104 printf -v hex "%x" "$sizeDump" 105 x=${#hex} 106 if [ $(($x % 2)) -eq 1 ]; then 107 hex=0$hex 108 x=${#hex} 109 fi 110 msize=`expr $x / 2` 111 msize=`expr $SIZE_8 - $msize` 112 add_null $msize 113 for ((i=0;i<$x;i+=2)); 114 do 115 printf \\x${hex:$i:2} >> $FILE 116 done 117} 118 119#Function to set dump id to 8 bytes format 120function get_dump_id() { 121 # shellcheck disable=SC2154 122 size=${#dump_id} 123 if [ "$1" == "$OP_DUMP" ]; then 124 nulltoadd=$(( SIZE_4 - size / 2 - size % 2 )) 125 add_null "$nulltoadd" 126 for ((i=0;i<size;i+=2)); 127 do 128 # shellcheck disable=SC2059 129 printf "\\x${dump_id:$i:2}" >> "$FILE" 130 done 131 else 132 nulltoadd=$(( SIZE_8 - size )) 133 printf '%*s' "$nulltoadd" | tr ' ' "0" >> "$FILE" 134 printf "%s" "$dump_id" >> "$FILE" 135 fi 136} 137 138#Function to get the bmc serial number 139function getbmc_serial() { 140 x=${#bmcSerialNo} 141 nulltoadd=`expr $SIZE_12 - $x` 142 printf $bmcSerialNo >> $FILE 143 printf '%*s' $nulltoadd | tr ' ' "0" >> $FILE 144} 145 146#Function to fetch the hostname 147function system_name() { 148 name=$(hostname) 149 len=${#name} 150 nulltoadd=$(( SIZE_32 - len )) 151 printf "%s" "$name" >> "$FILE" 152 add_null "$nulltoadd" 153} 154 155#Function to get the errorlog ID 156function get_eid() { 157 # shellcheck disable=SC2154 # dump_type comes from elsewhere 158 if [ "$dump_type" = "$OP_DUMP" ]; then 159 # shellcheck disable=SC2154 # elog_id comes from elsewhere 160 x=${#elog_id} 161 if [ "$x" = 8 ]; then 162 msize=$(( x / 2 )) 163 msize=$(( SIZE_4 - msize )) 164 for ((i=0;i<x;i+=2)); 165 do 166 printf "\\x${elog_id:$i:2}" >> "$FILE" 167 done 168 add_null "$msize" 169 else 170 add_null 4 171 fi 172 else 173 if ! { [[ $dump_type = "$TYPE_ELOG" ]] || \ 174 [[ $dump_type = "$TYPE_CHECKSTOP" ]]; }; then 175 x=${#elog_id} 176 if [ "$x" = 8 ]; then 177 for ((i=0;i<x;i+=2)); 178 do 179 printf "\\x${elog_id:$i:2}" >> "$FILE" 180 done 181 else 182 add_null 4 183 fi 184 else 185 strpelid=$(busctl get-property $PHOSPHOR_LOGGING \ 186 $optional_path $PEL_ENTRY $PEL_ID_PROP | cut -d " " -f 2) 187 decpelid=$(expr "$strpelid" + 0) 188 hexpelid=$(printf "%x" "$decpelid") 189 x=${#hexpelid} 190 if [ "$x" = 8 ]; then 191 for ((i=0;i<x;i+=2)); 192 do 193 printf "\\x${hexpelid:$i:2}" >> "$FILE" 194 done 195 else 196 add_null 4 197 fi 198 fi 199 fi 200} 201 202#Function to get the tar size of the dump 203function tar_size() { 204 printf -v hex "%x" "$size_dump" 205 x=${#hex} 206 if [ $((x % 2)) -eq 1 ]; then 207 hex=0$hex 208 x=${#hex} 209 fi 210 msize=$(( x / 2 )) 211 msize=$(( SIZE_4 - msize )) 212 add_null "$msize" 213 for ((i=0;i<x;i+=2)); 214 do 215 # shellcheck disable=SC2059 # using 'hex' as a variable is safe here. 216 printf "\\x${hex:$i:2}" >> "$FILE" 217 done 218} 219 220#Function will get the total size of the dump without header 221# i.e. Dump summary size i.e. 1024 bytes + the tar file size 222function total_size() { 223 size_dump=$(( size_dump + DUMP_SUMMARY_SIZE )) 224 printf -v hex "%x" "$size_dump" 225 x=${#hex} 226 if [ $((x % 2)) -eq 1 ]; then 227 hex=0$hex 228 x=${#hex} 229 fi 230 msize=$(( x / 2 )) 231 msize=$(( SIZE_8 - msize )) 232 add_null "$msize" 233 for ((i=0;i<x;i+=2)); 234 do 235 # shellcheck disable=SC2059 # using 'hex' as a variable is safe here. 236 printf "\\x${hex:$i:2}" >> "$FILE" 237 done 238} 239 240#Function to populate content type based on dump type 241function content_type() { 242 type="00000000" 243 # content type: 244 # Hostboot dump = "20" 245 # Hardware dump = "00" 246 # SBE dump = "30" 247 if [ "$dump_content_type" = "$HB_DUMP" ]; then 248 type="00000200" 249 elif [ "$dump_content_type" = "$HW_DUMP" ]; then 250 type="40000000" 251 elif [[ "$dump_content_type" = "$SBE_DUMP" || "$dump_content_type" = "$MSBE_DUMP" ]]; then 252 type="02000000" 253 fi 254 x=${#type} 255 for ((i=0;i<$x;i+=2)); 256 do 257 # shellcheck disable=SC2059 # using 'type' as a variable is safe here. 258 printf "\\x${type:$i:2}" >> "$FILE" 259 done 260} 261 262# @brief Fetching model number and serial number property from inventory 263# If the busctl command fails, populating the model and serial number 264# with default value i.e. 0 265function get_bmc_model_serial_number() { 266 modelNo=$(busctl get-property $INVENTORY_MANAGER $INVENTORY_PATH \ 267 $INVENTORY_ASSET_INT Model | cut -d " " -f 2 | sed "s/^\(\"\)\(.*\)\1\$/\2/g") 268 269 if [ -z "$modelNo" ]; then 270 modelNo="00000000" 271 fi 272 273 bmcSerialNo=$(busctl call $INVENTORY_MANAGER $INVENTORY_BMC_BOARD \ 274 org.freedesktop.DBus.Properties Get ss $INVENTORY_ASSET_INT \ 275 SerialNumber | cut -d " " -f 3 | sed "s/^\(\"\)\(.*\)\1\$/\2/g") 276 277 if [ -z "$bmcSerialNo" ]; then 278 bmcSerialNo="000000000000" 279 fi 280} 281 282#Function to add virtual file directory entry, consists of below entries 283####################FORMAT################ 284#Name Size(bytes) Value 285#Entry Header 8 FILE 286#Entry Size 2 0x0040 287#Reserved 10 NULL 288#Entry Type 2 0x0001 289#File Name Prefix 2 0x000F 290#Dump File Type 7 BMCDUMP/SYSDUMP/NAGDUMP 291#Separator 1 . 292#System Serial No 7 System serial number fetched from system 293#Dump Identifier 8 Dump Identifier value fetched from dump 294#Separator 1 . 295#Time stamp 14 Form should be yyyymmddhhmmss 296#Null Terminator 1 0x00 297function dump_file_entry() { 298 printf "FILE " >> $FILE 299 add_null 1 300 printf '\x40' >> $FILE #Virtual file directory entry size 301 add_null 11 302 printf '\x01' >> $FILE 303 add_null 1 304 printf '\x0F' >> "$FILE" 305 if [ "$dump_type" = "$OP_DUMP" ]; then 306 printf "SYSDUMP.%s." "$serialNo" >> "$FILE" 307 else 308 printf "%s.%s." "$header_dump_name" "$serialNo" >> "$FILE" 309 fi 310 get_dump_id 311 printf "." >> $FILE 312 printf $dDay >> $FILE #UTC time stamp 313 add_null 1 314} 315 316#Function section directory entry, consists of below entries 317####################FORMAT################ 318#Name Size(bytes) Value 319#Entry Header 8 SECTION 320#Entry Size 2 0x0030 321#Section Priority 2 0x0000 322#Reserved 4 NULL 323#Entry Flags 4 0x00000001 324#Entry Types 2 0x0002 325#Reserved 2 NULL 326#Dump Size 8 Dump size in hex + dump header 327#Optional Section 16 BMCDUMP/NAGDUMP/DUMP SUMMARY 328function dump_section_entry() { 329 printf "SECTION " >> $FILE 330 add_null 1 331 printf '\x30' >> "$FILE" #Section entry size 332 add_null 9 333 if [ "$dump_type" = "$OP_DUMP" ]; then 334 add_null 1 335 else 336 printf '\x01' >> "$FILE" 337 fi 338 add_null 1 339 printf '\x02' >> "$FILE" 340 add_null 2 341 if [ "$dump_type" = "$OP_DUMP" ]; then 342 add_null 6 343 printf '\x04' >> "$FILE" 344 add_null 1 345 printf "DUMP SUMMARY" >> "$FILE" 346 add_null 4 347 else 348 dump_size #Dump size 349 printf "%s" "$header_dump_name" >> "$FILE" 350 add_null 9 351 fi 352} 353 354#Function to add dump header, consists of below entries 355####################FORMAT################ 356#Name Size(bytes) Value 357#Dump type 8 BMC/NAG DUMP 358#Dump Request time 8 Dump request time stamp (in BCD) 359#Dump Identifier 4 Dump identifier fetched from dump 360#Dump version 2 0x0210 361#Dump header 2 0x200 362#Total dump size 8 Dump size + dump header 363#Panel function 32 System model, feature, type and IPL mode 364#System Name 32 System Name (in ASCII) 365#Serial number 7 System serial number 366#Reserved 1 NULL 367#PLID 4 Comes from errorlog 368#File Header Size 2 0x70 369#Dump SRC Size 2 Dump SRC Size. Currently NULL 370#DUMP SRC 320 DUMP SRC. Currently NULL 371#Dump Req Type 4 Dump requester user interface type. 372#Dump Req ID 32 Dump requester user interface ID 373#Dump Req user ID 32 Dump requester user ID. 374# 375#TODO: Github issue #2639, to populate the unpopulated elements. 376#Note: Unpopulated elements are listed below are set as NULL 377#PLID 378#SRC size 379#SRC dump 380#Dump requester type 381#Dump Req ID 382#Dump Req user ID 383function dump_header() { 384 if [ $dump_type = "$TYPE_FAULTDATA" ]; then 385 printf "FLT DUMP" >> $FILE 386 else 387 printf "BMC DUMP" >> $FILE 388 fi 389 dump_time 390 add_null 4 #Dump Identifier 391 printf '\x02' >> $FILE #Dump version 0x0210 392 printf '\x10' >> $FILE 393 printf '\x02' >> $FILE #Dump header size 0x0200 394 add_null 1 395 dump_size #dump size 396 printf "$modelNo" >> "$FILE" 397 add_null 24 398 printf "Server-%s-SN%s" "$modelNo" "$serialNo" >> "$FILE" 399 add_null 7 400 printf "$serialNo" >> "$FILE" 401 add_null 1 402 get_eid 403 add_null 1 404 printf '\x70' >> "$FILE" #File header size 405 add_null 2 # SRC size 406 add_null 320 # SRC dump 407 getbmc_serial 408 # Dump requester/Originator details 409 add_originator_details 410 add_null 32 # Dump Req user ID 411} 412 413#Function to add Dump entry, consists of below entries 414####################FORMAT################ 415#Name Size(bytes) Value 416#Dump Entry Version 1 0x01 417#BMC Dump Valid 1 0x01 418#No of Dump Entry 2 Number of Dump Entry 419# 420function dump_entry() { 421 printf '\x01' >> $FILE #Dump entry version 422 printf '\x01' >> $FILE #Dump valid 423 add_null 1 424 printf '\x10' >> $FILE #Dump entry 425} 426 427#Function to Hardware Dump Section 428####################FORMAT################## 429#Name Size(bytes) Value 430#HWDumpHeader 8 SECTION 431#HWDumpEntrySize 2 0x0030 432#HWDumpPriority 2 0x02 433#reserve6 4 NULL 434#HWDumpEntryFlag 4 0x0001 435#HWDumpEntryType 2 0x02 436#reserve7 2 NULL 437#reserve7a 4 NULL 438#HWDumpSize 4 NULL 439#HWDumpName[16] 16 HARDWARE DATA 440function hw_dump_section() { 441 printf "SECTION " >> "$FILE" 442 add_null 1 443 printf '\x30' >> "$FILE" #Section entry size 444 add_null 1 445 printf '\x02' >> "$FILE" 446 add_null 7 447 printf '\x00' >> "$FILE" 448 add_null 1 449 printf '\x02' >> "$FILE" 450 add_null 6 451 tar_size 452 printf "HARDWARE DATA" >> "$FILE" 453 add_null 3 454} 455 456#Function to Mainstore Dump Section 457######################FORMAT################### 458#Name Size(in bytes) Value 459#MainstoreHeader 8 SECTION 460#MainstoreEntrySize 2 0x30 461#MainstorePriority 2 0x02 462#reserve8 4 NULL 463#MainstoreEntryFlag 4 NULL 464#MainstoreEntryType 2 0x01 465#reserve9 2 NULL 466#MainstoreSize 8 NULL 467#MainstoreName 16 HYPERVISOR DATA 468function mainstore_dump_section() { 469 printf "SECTION " >> "$FILE" 470 add_null 1 471 printf '\x30' >> "$FILE" #Section entry size 472 add_null 1 473 printf '\x02' >> "$FILE" 474 add_null 7 475 printf '\x01' >> "$FILE" 476 add_null 1 477 printf '\x02' >> "$FILE" 478 add_null 10 479 printf "HYPERVISOR DATA" >> "$FILE" 480 add_null 1 481} 482 483#Function for platform system dump header 484######################FORMAT################## 485#Name Size(in bytes) Value 486#eyeCatcher 8 SYS DUMP 487#requestTimestamp 8 BCD time 488#dumpIdentifier 4 489#dumpVersion 2 490#headerSize 2 491#totalDumpSize 8 492#machineInfo 32 493#systemName 32 494#systemSerialNumber 7 495#Dump Creator BMC 1 496#eventLogId 4 497#fileHeaderSize 2 498####################DATA NOT AVAILABLE########## 499#srcSize 2 500#dumpSrc 332 501#toolType 4 502#clientId 32 503#userId 32 504#systemDumpFlags 2 505#hardwareErrorFlags 2 506#processorChipEcid 2 507#hardwareObjectModel 1 508#chUnused 1 509#cecMemoryErrorFlags 8 510#memoryDumpStartTime 8 511#memoryDumpCompleteTime 8 512#hypVerRelMod 8 513#Reserved4HysrInfo 2 514#hypMode 1 515#hypDumpContentPolicy 1 516#Reserved4HWDInfo 2 517#hardwareNodalCount 2 518#hardwareDumpTableSize 4 519#hardwareDumpDataSize 4 520#totalHardwareDumpDataSize 4 521#mdrtTableSize 4 522#mdrtTableAddress 8 523#memoryDumpDataSize 8 524#hypModLoadMapTime 8 525#hardwareCollectionEndTime 8 526#contentType 4 527#failingUnitId 4 528#failingCappChipId 2 529#failingCappUnitId 1 530#reserve02 5 531#hypHRMOR 8 532#hypNACAAddress 8 533#hardwareCollectionStartTime 8 534#mdstTableSize 4 535#payloadState 4 536#creator BMC 1 537#reservedForCreator 169 538function plat_dump_header() { 539 printf "SYS DUMP" >> "$FILE" 540 dump_time 541 get_dump_id "$OP_DUMP" #Dump identifier 542 printf '\x02' >> "$FILE" 543 printf '\x21' >> "$FILE" #Need to cross check 544 printf '\x04' >> "$FILE" #Dump header size 0x0400 545 add_null 1 546 total_size 547 printf "%s" "$modelNo" >> "$FILE" 548 add_null 24 549 system_name 550 printf "%s" "$serialNo" >> "$FILE" 551 printf '\x01' >> "$FILE" #Dump Creator BMC 552 get_eid 553 add_null 1 554 printf '\xd0' >> "$FILE" #File Header size 555 add_null 498 556 content_type # 4 bytes 557 add_null 44 558 printf '\x01' >> "$FILE" # BMC indicator 559 add_null 367 560} 561 562 563#main function 564function gen_header_package() { 565 fetch_serial_number 566 dump_file_entry 567 dump_section_entry 568 if [ "$dump_type" = "$OP_DUMP" ]; then 569 hw_dump_section 570 mainstore_dump_section 571 plat_dump_header 572 else 573 dump_header 574 dump_entry 575 fi 576} 577 578get_bmc_model_serial_number 579 580#Run gen_header_package 581gen_header_package 582