xref: /openbmc/openpower-debug-collector/dump/tools/common/include/opfunctions (revision da97c0678c175429ba4796d4896e0c5054783fc6)
1#!/usr/bin/env bash
2
3declare -rx TYPE_FAULTDATA="faultdata"
4#Dump originator variables
5declare -x ORIGINATOR_TYPE=""
6declare -x ORIGINATOR_ID=""
7
8# @brief fetch serial number
9# @param serial number
10function fetch_serial_number() {
11    serialNo=$(busctl get-property xyz.openbmc_project.Inventory.Manager \
12            /xyz/openbmc_project/inventory/system xyz.openbmc_project.Inventory.Decorator.Asset \
13        SerialNumber | cut -d " " -f 2 | sed "s/^\(\"\)\(.*\)\1\$/\2/g")
14
15    echo "Fetched SerialNumber : ${serialNo}"
16    if [[ -z "$serialNo" || ! $serialNo =~ ^[A-Za-z0-9]+$ ]]; then
17        serialNo="0000000"
18    else
19        # Pad to 7 characters with leading zeros
20        serialNo=$(printf "%07s" "$serialNo" | tr ' ' '0')
21    fi
22}
23
24# @brief Add BMC dump File Name
25# @param BMC Dump File Name
26function get_bmc_dump_filename() {
27    fetch_serial_number
28    dump_id=$(printf %08d $dump_id)
29    if [ $dump_type = "$TYPE_FAULTDATA" ]; then
30        header_dump_name="FLTDUMP"
31        name="NAGDUMP.${serialNo}.${dump_id}.${dDay}"
32    else
33        header_dump_name="BMCDUMP"
34        name="BMCDUMP.${serialNo}.${dump_id}.${dDay}"
35    fi
36}
37
38# @brief Function to get the Originator details
39# @param Originator Type and Originator ID
40function get_originator_details() {
41    if [ -z "$dump_dir" ]; then
42        return
43    fi
44
45    dump_type_received=""
46    dump_entry_id="$dump_id"
47
48    if [ "$1" = "bmc" ] || [ "$1" = "system" ]; then
49        dump_type_received="$1"
50    else
51        echo "Invalid dump type received"
52        return
53    fi
54
55    if [ "$dump_type_received" = "bmc" ]; then
56        dump_entry_id=$(echo "$dump_entry_id" | sed "s/^0*//")
57    fi
58
59    local DBUS_DUMP_MANAGER="xyz.openbmc_project.Dump.Manager"
60    local DBUS_DUMP_PATH="/xyz/openbmc_project/dump/$dump_type_received/entry/$dump_entry_id"
61    local DBUS_DUMP_ORIGINATOR_IFACE="xyz.openbmc_project.Common.OriginatedBy"
62    local DBUS_ORIGINATOR_TYPE_STRING="OriginatorType"
63    local DBUS_ORIGINATOR_ID_STRING="OriginatorId"
64
65    ORIGINATOR_TYPE=$(busctl get-property "$DBUS_DUMP_MANAGER" \
66            "$DBUS_DUMP_PATH" "$DBUS_DUMP_ORIGINATOR_IFACE" \
67        "$DBUS_ORIGINATOR_TYPE_STRING")
68
69    ORIGINATOR_ID=$(busctl get-property "$DBUS_DUMP_MANAGER" \
70            "$DBUS_DUMP_PATH" "$DBUS_DUMP_ORIGINATOR_IFACE" \
71        "$DBUS_ORIGINATOR_ID_STRING")
72
73    # The following two lines would extract the originator type and id
74    # from the received long string in response of the above dbus calls
75    # like <s "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal">
76    # to only <Internal> for the originator type and so on for the originator ID
77    ORIGINATOR_TYPE=$(echo "$ORIGINATOR_TYPE" | cut -d' ' -f 2 \
78        | cut -d'.' -f 6 | cut -d'"' -f 1)
79
80    ORIGINATOR_ID=$(echo "$ORIGINATOR_ID" | cut -d' ' -f 2 \
81        | cut -d'"' -f 2)
82
83    # This hash map for Originator Type is populated based on
84    # the info provided by the OriginatedBy.interface.yaml file under
85    # https://github.com/openbmc/phosphor-dbus-interfaces/
86    # Feel free to amend the table as per the evolving requirement
87    local -A originator_type_enum_map
88    originator_type_enum_map["Client"]=0
89    originator_type_enum_map["Internal"]=1
90    originator_type_enum_map["SupportingService"]=2
91
92    local originator_type_mapped="$ORIGINATOR_TYPE"
93    # If the originator type comes something which is not known to
94    # the enum list/map then make it blank so that can be filled
95    # with null bytes in gendumpheader script and won't be
96    # breaking the dump extraction
97    ORIGINATOR_TYPE=""
98    for key in "${!originator_type_enum_map[@]}"
99    do
100        if [ "$key" = "$originator_type_mapped" ]; then
101            ORIGINATOR_TYPE="${originator_type_enum_map[$key]}"
102            break
103        fi
104    done
105}
106