{%
    local PMC_3152_8I_SMART_RAID = 64 -- PMC 3152-8i SmartRAID
    local PMC_2100_8I_SMART_HBA = 65  -- PMC 2100-8i SmartHBA
    local match_flag = false -- 单个物理盘匹配成功标志
    -- 物理盘的健康状态
    local function get_health_status(health)
        local str_char_033 = string.char(0x1b)
        if health == 0 then
            return string.format('%s[1;32m%s%s[0m', str_char_033, 'Normal', str_char_033)
        elseif health == 1 then
            return string.format('%s[1;33m%s%s[0m', str_char_033, 'Minor', str_char_033)
        elseif health == 2 then
            return string.format('%s[1;33m%s%s[0m', str_char_033, 'Major', str_char_033)
        elseif health == 3 then
            return string.format('%s[1;31m%s%s[0m', str_char_033, 'Critical', str_char_033)
        else
            return "Unknown"
        end
    end
    -- 物理盘的FW状态
    local function get_physical_drive_fw_status(fw_status)
        local fw_status_string = {
            [0] = 'UNCONFIGURED GOOD',
            [1] = 'UNCONFIGURED BAD',
            [2] = 'HOT SPARE',
            [3] = 'OFFLINE',
            [4] = 'FAILED',
            [5] = 'REBUILD',
            [6] = 'ONLINE',
            [7] = 'COPYBACK',
            [8] = 'JBOD',
            [9] = 'UNCONFIGURED(Shielded)',
            [10] = 'HOT SPARE(Shielded)',
            [11] = 'CONFIGURED(Shielded)',
            [12] = 'FOREIGN',
            [13] = 'ACTIVE',
            [14] = 'STAND-BY',
            [15] = 'SLEEP',
            [16] = 'DST executing in background',
            [17] = 'SMART Off-line Data Collection executing in background',
            [18] = 'SCT command executing in background',
            [19] = 'ONLINE',
            [20] = 'RAW',
            [21] = 'READY',
            [22] = 'NOT SUPPORTED',
            [23] = 'PREDICTIVE FAILURE',
            [24] = 'DIAGNOSING',
            [25] = 'INCOMPATIBLE',
            [26] = 'ERASE IN PROGRESS'
        }
        if fw_status_string[fw_status] then
            return fw_status_string[fw_status]
        elseif fw_status == 255 then
            return 'N/A'
        else
            return string.format('%s(0x%02X)', 'UNDEFINED', fw_status)
        end
    end
    -- 获取物理盘的power状态
    local function get_physical_drive_power_state(power_state)
        if power_state == 0 then
            return 'Spun Up'
        elseif power_state == 1 then
            return 'Spun Down'
        elseif power_state == 2 then
            return 'Transition'
        else
            return 'N/A'
        end
    end
    -- 获取物理盘媒介类型
    local function get_physical_drive_media_type(media_type)
        if media_type == 0 then
            return 'HDD'
        elseif media_type == 1 then
            return 'SSD'
        elseif media_type == 2 then
            return 'SSM'
        else
            return 'N/A'
        end
    end
    -- 物理盘的接口类型
    local function get_physical_drive_interface_type(if_type)
        local interface_type_string = {
            [0] = 'Unknown',
            [1] = 'Parallel SCSI',
            [2] = 'SAS',
            [3] = 'SATA',
            [4] = 'Fiber Channel',
            [5] = 'SATA/SAS',
            [6] = 'PCIe'
        }
        if type(if_type) == 'number' and if_type >= 0 and if_type <= 6 then
            return interface_type_string[if_type]
        else
            return 'N/A'
        end
    end
    -- 物理盘的接口速率
    local function get_physical_drive_speed(speed)
        local speed_string = {
            "1.5 Gbps", "3.0 Gbps", "6.0 Gbps", "12.0 Gbps",
            "2.5 Gbps", "5.0 Gbps", "8.0 Gbps", "10.0 Gbps",
            "16.0 Gbps", "20.0 Gbps", "30.0 Gbps", "32.0 Gbps",
            "40.0 Gbps", "64.0 Gbps", "80.0 Gbps", "96.0 Gbps",
            "128.0 Gbps", "160.0 Gbps", "256.0 Gbps", "22.5 Gbps", 'N/A'
        }
        if speed >= 1 and speed <= 20 then
            return speed_string[speed]
        else
            return 'N/A'
        end
    end
    -- 获取容量大小，根据容量大小自动选择单位
    local function get_capacity(size)
        local capacity_basic_unit = 1024
        local STORAGE_INFO_INVALID_DWORD = 0xFFFFFFFF
        if size == STORAGE_INFO_INVALID_DWORD then
            return 'N/A'
        elseif size < capacity_basic_unit then -- 小于1TB
            return string.format('%d MB', size)
        elseif size < capacity_basic_unit * capacity_basic_unit then -- 小于1TB
            return string.format('%.3f GB', size / capacity_basic_unit)
        elseif size < capacity_basic_unit * capacity_basic_unit * capacity_basic_unit then -- 小于1PB
            return string.format('%.3f TB', size / (capacity_basic_unit * capacity_basic_unit))
        else
            return string.format('%.3f PB', size / (capacity_basic_unit * capacity_basic_unit * capacity_basic_unit))
        end
    end
    -- 物理盘的热备状态
    local hot_spare_state_string = {
        [0] = 'None',
        [1] = 'Global',
        [2] = 'Dedicated',
        [3] = 'Auto Replace',
        [255] = 'N/A'
    }

    local function get_rebuild_state(rebuild_state, rebuild_progress, type_id)
        if rebuild_state == 0xFF then
            return 'N/A'
        elseif rebuild_state == 0 then
            return 'No'
        elseif rebuild_state == 1 then
            if rebuild_progress == 0xFF then
                if type_id == PMC_3152_8I_SMART_RAID or type_id == PMC_2100_8I_SMART_HBA then
                    return 'Yes (N/A)'
                else
                    return 'Yes (Unknown Progress)'
                end
            else
                return string.format('%s (%d%%)', 'Yes', rebuild_progress)
            end
        else
            return 'Unknown'
        end
    end
    -- 巡检状态
    local function get_physical_drive_patrol_state(patrol_state)
        if patrol_state == 0 then
            return 'No'
        elseif patrol_state == 1 then
            return 'Yes'
        else
            return 'N/A'
        end
    end
    -- 硬盘剩余磨损率，即剩余擦写次数的百分比（0-100，或者255表示无效）
    local function get_remnant_media_weraout(remnant_media_wearout)
        if remnant_media_wearout == 0xFF then
            return 'N/A'
        else
            return remnant_media_wearout .. '%'
        end
    end
    -- 上电时间
    local function get_physical_drive_power_on_hours(hours)
        if hours >= 0xFFFF then
            return 'N/A'
        else
            return hours
        end
    end
    -- 物理盘的定位状态
    local function get_physical_locate_state(locate_led, fault)
        local locate_state

        if fault == 0 and locate_led == 1 then
            locate_state = 1 -- location started
        elseif fault == 0xFF or locate_led == 0xFF then
            locate_state = 0xFF -- location unknnown
        else
            locate_state = 0 -- location stopped
        end

        if locate_state == 0xFF then
            return 'N/A'
        elseif locate_state == 0 then
            return 'Off'
        else
            return 'On'
        end
    end
    -- 是否是启动盘
    local function get_drive_bootable(bootable)
        if bootable > 0 and bootable ~= 0xff then
            return 'Yes'
        elseif bootable == 0 then
            return 'No'
        else
            return 'N/A'
        end
    end
    -- 启动盘优先级
    local function get_drive_boot_priority(boot_priority)
        if boot_priority == 0 then
            return 'None'
        elseif boot_priority == 1 then
            return 'Primary'
        elseif boot_priority == 2 then
            return 'Secondary'
        elseif boot_priority == 3 then
            return 'All'
        else
            return 'N/A'
        end
    end
    local function get_physical_drive_err_count(count)
        if count ~= 0xFFFFFFFF then
            return count
        else
            return 'N/A'
        end
    end
    -- 单个物理盘的信息
    function print_physical_drive_info(item)
        echo('Physical Drive Information\n')
        echo('----------------------------------------------------------------------\n')
        echo('ID                                       : ' .. item.Id, '\n')
        echo('Device Name                              : ' .. item.Name, '\n')
        echo('Manufacturer                             : ' .. item.Manufacturer, '\n')
        echo('Serial Number                            : ' .. item.SerialNumber, '\n')
        echo('Model                                    : ' .. item.Model, '\n')
        echo('Firmware Version                         : ' .. item.Revision, '\n')
        echo('Health Status                            : ' .. get_health_status(item.Health), '\n')
        echo('Firmware State                           : ' .. get_physical_drive_fw_status(item.FirmwareStatus), '\n')
        if item.TypeId ~= PMC_3152_8I_SMART_RAID then
            echo('Power State                              : ' .. get_physical_drive_power_state(item.PowerState), '\n')
        end
        echo('Media Type                               : ' .. get_physical_drive_media_type(item.MediaType), '\n')
        echo('Interface Type                           : ' .. get_physical_drive_interface_type(item.Protocol), '\n')
        echo('Capable Speed                            : ' .. get_physical_drive_speed(item.CapableSpeedGbs), '\n')
        echo('Negotiated Speed                         : ' .. get_physical_drive_speed(item.NegotiatedSpeedGbs), '\n')
        if item.TemperatureCelsius ~= 0xFF then
            echo(string.format('%-40s : %d(Celsius)', 'Drive Temperature', item.TemperatureCelsius), '\n')
        else
            echo('Drive Temperature                        : N/A', '\n')
        end
        echo('Capacity                                 : ' .. get_capacity(item.CapacityMiB), '\n')
        echo('Hot Spare                                : ' .. hot_spare_state_string[item.HotspareType], '\n')
        echo('Rebuild in Progress                      : ' .. get_rebuild_state(item.RebuildState, item.RebuildProgress, item.TypeId), '\n')
        if item.TypeId ~= PMC_3152_8I_SMART_RAID then
            echo('Patrol Read in Progress                  : ' .. get_physical_drive_patrol_state(item.PatrolState), '\n')
        end
        echo('Remnant Media Wearout                    : ' .. get_remnant_media_weraout(item.PredictedMediaLifeLeftPercent), '\n')
        echo('Power-On Hours                           : ' .. get_physical_drive_power_on_hours(item.PowerOnHours), '\n')
        echo('SAS Address(0)                           : ' .. item.SASAddress1, '\n')
        echo('SAS Address(1)                           : ' .. item.SASAddress2, '\n')
        echo('Location State                           : ' .. get_physical_locate_state(item.LocateLed, item.FaultLed), '\n')
        echo('Bootable                                 : ' .. get_drive_bootable(item.BootPriority), '\n')
        echo('Boot Priority                            : ' .. get_drive_boot_priority(item.BootPriority), '\n')
        echo('\n')
        if item.TypeId ~= PMC_3152_8I_SMART_RAID then
            echo('Media Error Count                        : ' .. get_physical_drive_err_count(item.MediaErrorCount), '\n')
            echo('Prefail Error Count                      : ' .. get_physical_drive_err_count(item.PredictedFailCount), '\n')
            echo('Other Error Count                        : ' .. get_physical_drive_err_count(item.OtherErrorCount), '\n')
        end
        echo('----------------------------------------------------------------------\n\n')
    end
    -- 输出所有物理盘
    if option == 'all' then
        if #GetPhysicalDriveList == 0 then
            echo('Not Found Physical Drive.')
        else
            table.sort(GetPhysicalDriveList, function(a, b)
                return a.Id < b.Id
            end)
            for _, item in pairs(GetPhysicalDriveList) do
                if item.Presence == 1 then
                    print_physical_drive_info(item)
                    match_flag = true
                end
            end
            -- 不存在在位的物理盘
            if match_flag ~= true then
                echo('Not Found Physical Drive.')
            end
        end
    -- 输出单个物理盘
    else
        if GetOptionValid ~= true then
            echo('Invalid Physical Drive ID.')
        elseif #GetPhysicalDriveList == 0 then
            echo('Not Found Physical Drive.')
        else
            for _, item in ipairs(GetPhysicalDriveList) do
                if math.tointeger(item.Name:sub(5)) == math.tointeger(option) then -- 指定的physical drive
                    match_flag = true
                    if item.Presence == 0 then -- 物理盘不在位
                        echo('The physical drive is not in position.\n')
                        echo('Physical Drive Information\n')
                        echo('----------------------------------------------------------------------\n')
                        echo('ID                                       : ' .. item.Id,   '\n')
                        echo('Device Name                              : ' .. item.Name, '\n')
                        echo('----------------------------------------------------------------------\n')
                    else
                        print_physical_drive_info(item)
                    end
                    break
                end
            end
            if match_flag ~= true then
                echo('Invalid Physical Drive ID : ' .. option)
            end
        end
    end
    echo('\b')
%}
