psu_decoder.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. def decode_psu_status(hex_input):
  2. """
  3. PSU Decoder by Parv Ashwani
  4. ---------------------------
  5. Decodes a PSU status hex value into human-readable descriptions
  6. based on standard IPMI bitmask definitions.
  7. """
  8. bitmask_definitions = {
  9. 1: "Presence Detected",
  10. 2: "Failure Detected",
  11. 4: "Predictive Failure",
  12. 8: "Input Lost",
  13. 16: "Out of Range",
  14. 32: "Detected Out of Range",
  15. 64: "Configuration Error",
  16. 128: "Reserved"
  17. }
  18. print("\n" + "="*50)
  19. print(f"DECODING INPUT: {hex_input}")
  20. print("="*50)
  21. try:
  22. decimal_value = int(hex_input, 16)
  23. except ValueError:
  24. print("Error: Invalid hex input.")
  25. return
  26. print(f"\n[Step 1] Hex to Decimal Conversion:")
  27. print(f" Hex '{hex_input}' --> Decimal {decimal_value}")
  28. active_flags = []
  29. math_parts = []
  30. print(f"\n[Step 2] Bitwise Analysis (Checking switches):")
  31. for bit_value, description in sorted(bitmask_definitions.items(), reverse=True):
  32. if (decimal_value & bit_value) == bit_value:
  33. status = "ON"
  34. active_flags.append(description)
  35. math_parts.append(str(bit_value))
  36. print(f" - Checking Bit {bit_value:>3}: {status} --> Found: {description}")
  37. else:
  38. # Optional: Uncomment below line to see checks that failed
  39. # print(f" - Checking Bit {bit_value:>3}: OFF")
  40. pass
  41. print(f"\n[Step 3] Calculation Explanation:")
  42. if math_parts:
  43. equation = " + ".join(math_parts)
  44. print(f" Math: {equation} = {decimal_value}")
  45. else:
  46. print(" No active flags found (Value is 0).")
  47. print(f"\n[RESULT]:")
  48. if active_flags:
  49. for flag in active_flags:
  50. print(f" >> {flag}")
  51. else:
  52. print(" >> No active status flags.")
  53. # ==========================================
  54. # MAIN LOOP
  55. # ==========================================
  56. if __name__ == "__main__":
  57. print("--- PSU Status Decoder Tool ---")
  58. while True:
  59. user_input = input("\nEnter Hex value (e.g., 0xb) or 'q' to quit: ").strip()
  60. if user_input.lower() == 'q':
  61. print("Exiting...")
  62. print("Made with ☕︎ by Parv")
  63. break
  64. if user_input:
  65. decode_psu_status(user_input)