smbios.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*++
  2. Copyright (c) 2000 Intel Corporation
  3. Module Name:
  4. Smbios.c
  5. Abstract:
  6. Lib fucntions for SMBIOS. Used to get system serial number and GUID
  7. Revision History
  8. --*/
  9. #include "lib.h"
  10. /*
  11. * We convert 32 bit values to pointers. In 64 bit mode the compiler will issue a
  12. * warning stating that the value is too small for the pointer:
  13. * "warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]"
  14. * we can safely ignore them here.
  15. */
  16. #pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
  17. EFI_STATUS
  18. LibGetSmbiosSystemGuidAndSerialNumber (
  19. IN EFI_GUID *SystemGuid,
  20. OUT CHAR8 **SystemSerialNumber
  21. )
  22. {
  23. EFI_STATUS Status;
  24. SMBIOS_STRUCTURE_TABLE *SmbiosTable;
  25. SMBIOS_STRUCTURE_POINTER Smbios;
  26. SMBIOS_STRUCTURE_POINTER SmbiosEnd;
  27. UINT16 Index;
  28. Status = LibGetSystemConfigurationTable(&SMBIOSTableGuid, (VOID**)&SmbiosTable);
  29. if (EFI_ERROR(Status)) {
  30. return EFI_NOT_FOUND;
  31. }
  32. Smbios.Hdr = (SMBIOS_HEADER *)SmbiosTable->TableAddress;
  33. SmbiosEnd.Raw = (UINT8 *)(SmbiosTable->TableAddress + SmbiosTable->TableLength);
  34. for (Index = 0; Index < SmbiosTable->TableLength ; Index++) {
  35. if (Smbios.Hdr->Type == 1) {
  36. if (Smbios.Hdr->Length < 0x19) {
  37. //
  38. // Older version did not support Guid and Serial number
  39. //
  40. continue;
  41. }
  42. //
  43. // SMBIOS tables are byte packed so we need to do a byte copy to
  44. // prevend alignment faults on IA-64.
  45. CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof(EFI_GUID));
  46. *SystemSerialNumber = LibGetSmbiosString(&Smbios, Smbios.Type1->SerialNumber);
  47. return EFI_SUCCESS;
  48. }
  49. //
  50. // Make Smbios point to the next record
  51. //
  52. LibGetSmbiosString (&Smbios, -1);
  53. if (Smbios.Raw >= SmbiosEnd.Raw) {
  54. //
  55. // SMBIOS 2.1 incorrectly stated the length of SmbiosTable as 0x1e.
  56. // given this we must double check against the lenght of
  57. /// the structure. My home PC has this bug.ruthard
  58. //
  59. return EFI_SUCCESS;
  60. }
  61. }
  62. return EFI_SUCCESS;
  63. }
  64. CHAR8*
  65. LibGetSmbiosString (
  66. IN SMBIOS_STRUCTURE_POINTER *Smbios,
  67. IN UINT16 StringNumber
  68. )
  69. /*++
  70. Return SMBIOS string given the string number.
  71. Arguments:
  72. Smbios - Pointer to SMBIOS structure
  73. StringNumber - String number to return. -1 is used to skip all strings and
  74. point to the next SMBIOS structure.
  75. Returns:
  76. Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == -1
  77. --*/
  78. {
  79. UINT16 Index;
  80. CHAR8 *String;
  81. //
  82. // Skip over formatted section
  83. //
  84. String = (CHAR8 *)(Smbios->Raw + Smbios->Hdr->Length);
  85. //
  86. // Look through unformated section
  87. //
  88. for (Index = 1; Index <= StringNumber; Index++) {
  89. if (StringNumber == Index) {
  90. return String;
  91. }
  92. //
  93. // Skip string
  94. //
  95. for (; *String != 0; String++);
  96. String++;
  97. if (*String == 0) {
  98. //
  99. // If double NULL then we are done.
  100. // Retrun pointer to next structure in Smbios.
  101. // if you pass in a -1 you will always get here
  102. //
  103. Smbios->Raw = (UINT8 *)++String;
  104. return NULL;
  105. }
  106. }
  107. return NULL;
  108. }