diff --git a/LoongArch/smbios.c b/LoongArch/smbios.c index 1e62893..8b840d3 100644 --- a/LoongArch/smbios.c +++ b/LoongArch/smbios.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "def.h" #ifdef PLATFORM_LA64 @@ -18,9 +19,27 @@ UINT32 nameoffset = 0x2F000; //nameheader addr UINT32 offset = 0x3D000; //smbiosheader addr UINT32 nameoffset = 0x4E000; //nameheader addr #endif +// smbios info range max size in byte +UINT32 grangemaxsize = 0x3000; -#pragma pack(1) +#define SMBIOS_STORE_VERSION 1 +#define SMBIOS_STORE_SIGNATURE 0x45534253 + +#define SMBIOS_STORE_TOTAL_LENGTH(a) ((sizeof(((SMBIOS_STORE *)(a))->Header) + ((SMBIOS_STORE *)(a))->Header.BodyLength)) +#define BODY_ALIGNMENT 4 +#define ALIGN_VALUE_ADDEND(Value, Alignment) (((Alignment) - (Value)) & ((Alignment) - 1U)) +#define ALIGN_VALUE(Value, Alignment) ((Value) + ALIGN_VALUE_ADDEND (Value, Alignment)) + +typedef enum { + bodytype_string = 0, + bodytype_uuid = 1 +} SMBIOS_STORE_BODYTYPE; + +typedef enum { + bodystatus_invalid = 0 +} SMBIOS_STORE_BODYSTATUS; +#pragma pack(1) typedef struct { UINT8 Type; UINT8 StrNum; @@ -30,6 +49,29 @@ typedef struct { UINT64 ResultL; UINT32 StrSize; } SmbiosHeader; + +typedef struct { + UINT32 Signature; + UINT16 Version; + UINT16 BodyNumber; + UINT32 BodyLength; + UINT16 BodyChecksum; + UINT16 HeaderChecksum; +} SMBIOS_STORE_HEADER; + +typedef struct { + UINT32 Length; + UINT8 SmbiosType; + UINT8 SmbiosStringIndex; + UINT8 BodyType; + UINT8 BodyStatus; + UINT8 Body[]; +} SMBIOS_STORE_BODY; + +typedef struct { + SMBIOS_STORE_HEADER Header; + SMBIOS_STORE_BODY BodyList[]; +} SMBIOS_STORE; #pragma pack() const char *commandLineHelp = @@ -43,7 +85,7 @@ const char *commandLineHelp = " spi -s 1 4 SerialNumber # update smbios type1 SerialNumber\n" " spi -s 1 5 SKUNumber # update smbios type1 SKUNumber\n" " spi -s 1 6 Family # update smbios type1 Family\n" - " spi -s 1 7 0011223344556677 8899aabbccddeeff # update smbios type1 UUID\n" + " spi -s 1 7 00112233-4455-6677-8899-AABBCCDDEEFF # update smbios type1 UUID\n" " spi -s 2 1 Manufacture # update smbios type2 Manufacture\n" " spi -s 2 2 Product # update smbios type2 Product\n" " spi -s 2 3 Version # update smbios type2 Version\n" @@ -63,29 +105,494 @@ VOID SpiFlashSafeWrite ( VOID *Buffer, UINTN Num ); +UINTN SpiFlashRead ( + UINTN Offset, + VOID *Buffer, + UINTN Num + ); + +UINT16 +CalculateSum16 ( + UINT16 *Buffer, + UINTN Length + ) +{ + UINT16 Sum; + UINTN Count; + UINTN Total; + + if (Buffer == NULL) return -1; + if (((UINTN)Buffer & 0x1) != 0) return -1; + if ((Length & 0x1) != 0) return -1; + if (Length > (0xFFFFFFFFFFFFFFFFULL - ((UINTN)Buffer) + 1)) return -1; + + Total = Length / sizeof (*Buffer); + for (Sum = 0, Count = 0; Count < Total; Count++) { + Sum = (UINT16)(Sum + *(Buffer + Count)); + } + + return Sum; +} + +int checksumstore (SMBIOS_STORE *smbiosstore) +{ + if (smbiosstore == NULL) { + return -1; + } + + smbiosstore->Header.BodyChecksum = CalculateSum16((UINT16 *)smbiosstore->BodyList, smbiosstore->Header.BodyLength); + smbiosstore->Header.HeaderChecksum = 0; + smbiosstore->Header.HeaderChecksum = CalculateSum16((UINT16 *)&smbiosstore->Header, sizeof(SMBIOS_STORE_HEADER)); + + return 0; +} + +int get_body_info (SMBIOS_STORE *smbiosstore, UINT32 *Length, UINT16 *Number) +{ + SMBIOS_STORE_BODY *Item; + UINT32 BodyLength = 0; + UINT16 BodyNumber = 0; + + if ((smbiosstore == NULL) || (Length == NULL) || (Number == NULL)) { + return -1; + } + + Item = smbiosstore->BodyList; + while ((Item->Length != 0) && (Item->Length != (UINT32)(-1)) && (BodyLength < smbiosstore->Header.BodyLength)) { + BodyLength += Item->Length; + BodyNumber ++; + Item = (SMBIOS_STORE_BODY *)((UINT8 *)Item + Item->Length); + } + *Length = BodyLength; + *Number = BodyNumber; + + return 0; +} + +int get_body_item (SMBIOS_STORE *smbiosstore, UINT8 Type, UINT8 Index, SMBIOS_STORE_BODY **FindBody) +{ + SMBIOS_STORE_BODY *Item; + SMBIOS_STORE_BODY *FindItem; + UINT32 BodyOffset; + + if ((smbiosstore == NULL) || (FindBody == NULL)) { + return -1; + } + + Item = smbiosstore->BodyList; + FindItem = NULL; + BodyOffset = 0; + while ((Item->Length != 0) && (Item->Length != (UINT32)(-1)) && (BodyOffset < smbiosstore->Header.BodyLength)) { + if (Item->SmbiosType == Type && Item->SmbiosStringIndex == Index) { + FindItem = Item; + break; + } + BodyOffset += Item->Length; + Item = (SMBIOS_STORE_BODY *)((UINT8 *)Item + Item->Length); + } + + if (FindItem != NULL) { + *FindBody = FindItem; + return 0; + } else { + return -1; + } +} + +int new_body_item (SMBIOS_STORE *smbiosstore, UINT8 Type, UINT8 Index, char *bodystring, SMBIOS_STORE_BODY **newbody) +{ + SMBIOS_STORE_BODY *body; + UINT32 bodylength; + + if ((smbiosstore == NULL) || (bodystring == NULL) || (newbody == NULL)) { + return -1; + } + + bodylength = ALIGN_VALUE (sizeof(SMBIOS_STORE_BODY) + strlen(bodystring) + 1, BODY_ALIGNMENT); + body = (SMBIOS_STORE_BODY *) malloc (bodylength); + if (body == NULL) { + return -1; + } + memset(body, 0, bodylength); + + // init new body + body->Length = bodylength; + body->SmbiosType = Type; + body->SmbiosStringIndex = Index; + if ((Type == 1) && (Index == 7)) { + body->BodyType = bodytype_uuid; + } else { + body->BodyType = bodytype_string; + } + body->BodyStatus = 0; + memcpy(body->Body, bodystring, strlen(bodystring)); -int spi_update_smbios(const char *addr, int command, const char **argv) + *newbody = body; + + return 0; +} + +int cleanup_body_item (SMBIOS_STORE *smbiosstore) +{ + SMBIOS_STORE *newstore; + SMBIOS_STORE_BODY *Item; + UINT32 BodyOffset = 0; + UINT32 BodyNumber = 0; + UINT32 BodyLength = 0; + + if (smbiosstore == NULL) { + return -1; + } + + newstore = (SMBIOS_STORE *) malloc (SMBIOS_STORE_TOTAL_LENGTH(smbiosstore)); + if (newstore == NULL) { + return -1; + } + memcpy (newstore, &smbiosstore->Header, sizeof(SMBIOS_STORE_HEADER)); + + Item = smbiosstore->BodyList; + while ((Item->Length != 0) && (Item->Length != (UINT32)(-1)) && (BodyOffset < smbiosstore->Header.BodyLength)) { + if ((Item->BodyStatus & (1 << bodystatus_invalid)) == 0) { + memcpy ((UINT8 *) newstore->BodyList + BodyLength, Item, Item->Length); + BodyNumber ++; + BodyLength += Item->Length; + } + BodyOffset += Item->Length; + Item = (SMBIOS_STORE_BODY *)((UINT8 *)Item + Item->Length); + } + + memset (smbiosstore, 0, SMBIOS_STORE_TOTAL_LENGTH(smbiosstore)); + memcpy (smbiosstore, newstore, sizeof(SMBIOS_STORE_HEADER) + BodyLength); + smbiosstore->Header.BodyLength = BodyLength; + smbiosstore->Header.BodyNumber = BodyNumber; + checksumstore(smbiosstore); + + free(newstore); + + return 0; +} + +int combine_body_item (SMBIOS_STORE *smbiosstore, SMBIOS_STORE_BODY *body, SMBIOS_STORE **newstore) +{ + SMBIOS_STORE *store; + UINT32 newlength; + + if ((smbiosstore == NULL) || (body == NULL) || (newstore == NULL)) { + return -1; + } + + newlength = SMBIOS_STORE_TOTAL_LENGTH(smbiosstore) + body->Length; + + // combine new body + store = (SMBIOS_STORE *) malloc (newlength); + if (store == NULL) { + return -1; + } + memcpy(store, smbiosstore, SMBIOS_STORE_TOTAL_LENGTH(smbiosstore)); + memcpy((UINT8 *)store + SMBIOS_STORE_TOTAL_LENGTH(smbiosstore), body, body->Length); + + // Reorganize the "body" data and remove the invalid data. + store->Header.BodyLength += body->Length; + store->Header.BodyNumber ++; + checksumstore(store); + + *newstore = store; + + return 0; +} + +int check_guid_format_valid(const char *s) +{ + if (!s) return -1; + + const char *p = s; + int dash_pos[] = {8, 13, 18, 23}; + int dash_idx = 0; + + for (int i = 0; i < 36; i++, p++) + { + if (*p == '\0') return -1; + if (dash_idx < 4 && i == dash_pos[dash_idx]) + { + if (*p != '-') return -1; + dash_idx++; + } + else + { + if (!isxdigit((unsigned char)*p)) + return -1; + } + } + + if (*p != '\0') return -1; + + return 0; +} + +int check_smbiosstore_valid(SMBIOS_STORE *smbiosstore) +{ + int status; + UINT32 BodyLength = 0; + UINT16 BodyNumber = 0; + UINT16 checksum16; + + if (smbiosstore->Header.Signature != SMBIOS_STORE_SIGNATURE) { + return -1; + } + + status = get_body_info(smbiosstore, &BodyLength, &BodyNumber); + if ((status == 0) && ((smbiosstore->Header.BodyLength != BodyLength) || (smbiosstore->Header.BodyNumber != BodyNumber))) { + return -1; + } + + checksum16 = smbiosstore->Header.HeaderChecksum; + smbiosstore->Header.HeaderChecksum = 0; + smbiosstore->Header.HeaderChecksum = CalculateSum16((UINT16 *)&smbiosstore->Header, sizeof(SMBIOS_STORE_HEADER)); + if (smbiosstore->Header.HeaderChecksum != checksum16) { + smbiosstore->Header.HeaderChecksum = checksum16; + return -1; + } + + checksum16 = CalculateSum16((UINT16 *)smbiosstore->BodyList, smbiosstore->Header.BodyLength); + if (smbiosstore->Header.BodyChecksum != checksum16) { + return -1; + } + + return 0; +} + +int smbiosstore_select_version () +{ + void *p = NULL; + unsigned long long devaddr; + UINT32 Signature; + + int fd = open ("/dev/mem", O_RDWR|O_SYNC); + if(fd < 0) { + printf("can't open file,please use root .\n"); + return 1; + } + + devaddr = 0x1fe001f0; + /*Transfer Virtul to Phy Addr*/ + p = vtpa(devaddr, fd); + SPI_REG_BASE = (UINTN)p; + + SpiFlashRead (offset, &Signature, sizeof(Signature)); + releaseMem(p); + close(fd); + + if (Signature == SMBIOS_STORE_SIGNATURE) { + return 1; + } else { + return 0; + } +} + +int smbiosstore_v0 (UINT8 Type, UINT8 StringIndex, char *RecordString) +{ + int status; + UINT32 Index; + SmbiosHeader *smbiosheader; + UINT32 size = 1024*4; + UINT32 namesize = 1024*4; + UINT64 s = 0; + UINT64 l = 0; + char uuid_str[18]; + + if ((Type == 1) && (StringIndex == 1)) Index = 0; + if ((Type == 1) && (StringIndex == 2)) Index = 1; + if ((Type == 1) && (StringIndex == 3)) Index = 2; + if ((Type == 1) && (StringIndex == 4)) Index = 3; + if ((Type == 1) && (StringIndex == 5)) Index = 4; + if ((Type == 1) && (StringIndex == 6)) Index = 5; + if ((Type == 1) && (StringIndex == 7)) Index = 6; + if ((Type == 2) && (StringIndex == 1)) Index = 7; + if ((Type == 2) && (StringIndex == 2)) Index = 8; + if ((Type == 2) && (StringIndex == 3)) Index = 9; + if ((Type == 2) && (StringIndex == 4)) Index = 10; + if ((Type == 2) && (StringIndex == 5)) Index = 11; + if ((Type == 3) && (StringIndex == 1)) Index = 12; + if ((Type == 3) && (StringIndex == 2)) Index = 13; + if ((Type == 3) && (StringIndex == 3)) Index = 14; + if ((Type == 3) && (StringIndex == 4)) Index = 15; + if ((Type == 3) && (StringIndex == 5)) Index = 16; + + offset += Index * size; + nameoffset += Index * namesize; + + smbiosheader = malloc(size); + if (smbiosheader == NULL) { + return -1; + } + memset(smbiosheader, 0, sizeof(SmbiosHeader)); + + //printf("(%x)-(0x%lx)-(0x%lx)\n", Index, offset, nameoffset); + + smbiosheader->Type = Type; + smbiosheader->StrNum = StringIndex; + smbiosheader->HOffset = offset; + smbiosheader->NOffset = nameoffset; + smbiosheader->StrSize = (strlen((char *)RecordString) + 1) * 2; + if (Index == 6) { + int i, j; + char *endptr1; + char *endptr2; + + memset(uuid_str, 0, sizeof(uuid_str)); + for (i = 0, j = 0; i < sizeof(uuid_str); i ++) + { + if (RecordString[i] == '-') + continue; + uuid_str[j++] = RecordString[i]; + } + s = strtoull((char *)uuid_str, &endptr1, 16); + + memset(uuid_str, 0, sizeof(uuid_str)); + for (i = 0, j = 0; i < sizeof(uuid_str); i ++) + { + if (RecordString[i+sizeof(uuid_str)] == '-') + continue; + uuid_str[j++] = RecordString[i+sizeof(uuid_str)]; + } + l = strtoull((char *)uuid_str, &endptr2, 16); + + if ((*endptr1 == '\0') && (*endptr2 == '\0')) { + // printf("(0x%lx)-(0x%lx)\n", s, l); + smbiosheader->ResultH = s; + smbiosheader->ResultL = l; + printf("\n"); + } else { + printf("Unsupport string! please retry!\n"); + return 1; + } + } else { + smbiosheader->ResultH = 0; + smbiosheader->ResultL = 0; + } + + SpiFlashSafeWrite(0 + nameoffset, RecordString, namesize); + SpiFlashSafeWrite(0 + offset, smbiosheader, sizeof(SmbiosHeader)); + printf("V0 Program OK.\n"); + + return 0; +} + +int smbiosstore_v1 (UINT8 Type, UINT8 StringIndex, char *RecordString) +{ + int status; + SMBIOS_STORE *smbiosstore; + SMBIOS_STORE_BODY *body; + UINT8 needcleanup; + UINT8 *prebuffer, *newbuffer, *tempbuffer; + UINT32 length; + UINTN i; + UINT32 smbioslength = grangemaxsize; + + body = NULL; + prebuffer = NULL; + newbuffer = NULL; + smbiosstore = NULL; + needcleanup = 0; + + prebuffer = malloc (sizeof(SMBIOS_STORE)); + smbiosstore = (SMBIOS_STORE *) prebuffer; + SpiFlashRead (offset, smbiosstore, sizeof(SMBIOS_STORE)); + if (smbiosstore->Header.Signature == SMBIOS_STORE_SIGNATURE) { + length = SMBIOS_STORE_TOTAL_LENGTH(smbiosstore); + prebuffer = malloc (length); + free(smbiosstore); + smbiosstore = (SMBIOS_STORE *) prebuffer; + SpiFlashRead (offset, smbiosstore, length); + } + if (check_smbiosstore_valid(smbiosstore) != 0) { + printf("init smbios store area!\n"); + memset(smbiosstore, 0, sizeof(SMBIOS_STORE)); + smbiosstore->Header.Signature = SMBIOS_STORE_SIGNATURE; + smbiosstore->Header.Version = SMBIOS_STORE_VERSION; + } else { + status = get_body_item (smbiosstore, Type, StringIndex, &body); + if ((status == 0) && (body != NULL)) { + body->BodyStatus |= (1 << bodystatus_invalid); + needcleanup = 1; + } + } + + body = NULL; + status = new_body_item (smbiosstore, Type, StringIndex, RecordString, &body); + if (status == -1) { + goto done; + } + + // combine new body + status = combine_body_item (smbiosstore, body, (SMBIOS_STORE **) &newbuffer); + if (status == -1) { + goto done; + } + smbiosstore = (SMBIOS_STORE *) newbuffer; + + // update smbiosstore->Header.BodyLength, smbiosstore->Header.BodyNumber and smbiosstore->BodyList + if (needcleanup) { + status = cleanup_body_item(smbiosstore); + if (status == -1) { + goto done; + } + } + + // check max length + if (SMBIOS_STORE_TOTAL_LENGTH(smbiosstore) > smbioslength) { + printf("smbioslength is out of range, 0x%x > 0x%x, please check !!! \n", SMBIOS_STORE_TOTAL_LENGTH(smbiosstore), smbioslength); + status = -1; + goto done; + } + + // + // Save to spi flash, try 3 times + // + tempbuffer = malloc (SMBIOS_STORE_TOTAL_LENGTH(smbiosstore)); + for (i = 0; i < 3; i ++) + { + SpiFlashSafeWrite(offset, smbiosstore, SMBIOS_STORE_TOTAL_LENGTH(smbiosstore)); + SpiFlashRead (offset, tempbuffer, SMBIOS_STORE_TOTAL_LENGTH(smbiosstore)); + if (memcmp(tempbuffer, smbiosstore, SMBIOS_STORE_TOTAL_LENGTH(smbiosstore)) == 0) { + break; + } + } + free (tempbuffer); + if (i == 3) { + printf("ERROR: V1 Program FAIL!\n"); + status = -1; + } else { + printf("V1 Program OK.\n"); + status = 0; + } + +done: + if (body) + free(body); + if (prebuffer) + free(prebuffer); + if (newbuffer) + free(newbuffer); + + return status; +} + +int spi_update_smbios(const char *addr, unsigned int maxsize, int command, const char **argv) { void * p = NULL; unsigned long long devaddr; char RecordName[0x40]; char RecordName1[0x40]; - wchar_t RecordName2[16]; - wchar_t RecordName3[16]; + char RecordName2[4096]; int status; - SmbiosHeader *smbiosheader; UINT64 t = 0; UINT64 n = 0; - UINT64 s = 0; - UINT64 l = 0; - UINT32 size = 1024*4; - UINT32 namesize = 1024*4; - UINT32 Index; - UINT8 temptype; - UINT8 tempnum; + int version; + + // check version on bios flash + version = smbiosstore_select_version(); - Index = 0; - smbiosheader = NULL; if(addr != NULL){ sscanf (addr,"%lx", &devaddr); devaddr &= 0xfffffffffffffff0ULL; @@ -94,6 +601,9 @@ int spi_update_smbios(const char *addr, int command, const char **argv) } else { devaddr = 0x1fe001f0; } + if (maxsize != 0) { + grangemaxsize = maxsize; + } int fd = open ("/dev/mem", O_RDWR|O_SYNC); if(fd < 0) { @@ -107,33 +617,32 @@ int spi_update_smbios(const char *addr, int command, const char **argv) printf("%s", commandLineHelp); + memset(RecordName, 0, sizeof(RecordName)); + memset(RecordName1, 0, sizeof(RecordName1)); + memset(RecordName2, 0, sizeof(RecordName2)); if (command) { - if (argv[0]) { - status = sscanf(argv[0],"%s",RecordName); - printf("param0: %s\n", RecordName); - } - if (argv[1]) { - status = sscanf(argv[1],"%s",RecordName1); - printf("param1: %s\n", RecordName1); - } - if (argv[2]) { - status = sscanf(argv[2],"%s",RecordName2); - printf("param2: %s\n", RecordName2); - } - if (argv[3]) { - status = sscanf(argv[3],"%s",RecordName3); - printf("param3: %s\n", RecordName3); - } - if (!(argv[0] && argv[1] && argv[2])) { - printf("This may be a mistake, string is null, please check !!! \n"); - goto done; - } + if (argv[0]) { + status = sscanf(argv[0],"%63[^\n]",RecordName); + printf("param0: %s\n", RecordName); + } + if (argv[1]) { + status = sscanf(argv[1],"%63[^\n]",RecordName1); + printf("param1: %s\n", RecordName1); + } + if (argv[2]) { + status = sscanf(argv[2],"%4095[^\n]",RecordName2); + printf("param2: %s\n", RecordName2); + } + if (!(argv[0] && argv[1] && argv[2])) { + printf("This may be a mistake, string is null, please check !!! \n"); + goto done; + } } printf("Please input smbios type (only support 1/2/3):"); if (!command) { - status = scanf("%s",RecordName); //1.smbios type + status = scanf("%63[^\n]%*c",RecordName); //1.smbios type } if (RecordName != NULL) { printf("RecordName: (%s)\n", RecordName); @@ -150,10 +659,10 @@ int spi_update_smbios(const char *addr, int command, const char **argv) printf("\n"); printf("Please input smbios string number (only support 1/2/3/4/5/6/7):"); - if (!command) { - status = scanf("%s",RecordName1); //2. smbios string number - } - if (RecordName1 != NULL) { + if (!command) { + status = scanf("%63[^\n]%*c",RecordName1); //2. smbios string number + } + if (RecordName1 != NULL) { printf("RecordName1: (%s)\n", RecordName1); n = atoi(RecordName1); printf("n: (%x)\n", n); @@ -167,96 +676,42 @@ int spi_update_smbios(const char *addr, int command, const char **argv) return 1; } - if ((t == 1) && (n == 1)) Index = 0; - if ((t == 1) && (n == 2)) Index = 1; - if ((t == 1) && (n == 3)) Index = 2; - if ((t == 1) && (n == 4)) Index = 3; - if ((t == 1) && (n == 5)) Index = 4; - if ((t == 1) && (n == 6)) Index = 5; - if ((t == 1) && (n == 7)) Index = 6; - if ((t == 2) && (n == 1)) Index = 7; - if ((t == 2) && (n == 2)) Index = 8; - if ((t == 2) && (n == 3)) Index = 9; - if ((t == 2) && (n == 4)) Index = 10; - if ((t == 2) && (n == 5)) Index = 11; - if ((t == 3) && (n == 1)) Index = 12; - if ((t == 3) && (n == 2)) Index = 13; - if ((t == 3) && (n == 3)) Index = 14; - if ((t == 3) && (n == 4)) Index = 15; - if ((t == 3) && (n == 5)) Index = 16; - - offset += Index * size; - nameoffset += Index * namesize; - - smbiosheader = malloc(sizeof(SmbiosHeader)); - if (smbiosheader == NULL) { - return 1; - } - - //printf("(%x)-(0x%lx)-(0x%lx)\n", Index, offset, nameoffset); - - smbiosheader->Type = t; - smbiosheader->StrNum = n; - smbiosheader->HOffset = offset; - smbiosheader->NOffset = nameoffset; - - if (Index == 6) { - printf("Please Input smbios HEX1(64):"); + if ((t == 1) && (n == 7)) { + printf("Please Input smbios GUID format string:"); } else { printf("Please Input smbios strings:"); } - if (!command) { - status = scanf("%s",RecordName2); //3.smbios string - } + if (!command) { + status = scanf("%4095[^\n]%*c",RecordName2); //3.smbios string + } + if ((RecordName2 != NULL) && (strlen((char *)RecordName2) >= 1)) { - printf("RecordName2: (%s)-(%d)\n", RecordName2, (strlen((char *)RecordName2) + 1) * 2); - smbiosheader->StrSize = (strlen((char *)RecordName2) + 1) * 2; - if (Index == 6) { - printf("Please Input smbios HEX2(64):"); - if (!command) { - status = scanf("%s",RecordName3); - } - if (RecordName3 != NULL) { - //printf("RecordName3: (%s)\n", RecordName3); - char *endptr1; - char *endptr2; - - s = strtoull((char *)RecordName2, &endptr1, 16); - l = strtoull((char *)RecordName3, &endptr2, 16); - - if ((*endptr1 == '\0') && (*endptr2 == '\0')) { - //printf("(0x%lx)-(0x%lx)\n", s, l); - smbiosheader->ResultH = s; - smbiosheader->ResultL = l; - printf("\n"); - } else { - printf("Unsupport string! please retry!\n"); - return 1; - } + printf("RecordName2: (%s)-(%d)\n", RecordName2, strlen((char *)RecordName2) + 1); + if ((t == 1) && (n == 7)) { + if (check_guid_format_valid (RecordName2)) { + printf("guid format error, eg: 00112233-4455-6677-8899-AABBCCDDEEFF, please check !!! \n"); + goto done; } - } else { - smbiosheader->ResultH = 0; - smbiosheader->ResultL = 0; } - SpiFlashSafeWrite(0 + nameoffset, RecordName2, namesize); - SpiFlashSafeWrite(0 + offset, smbiosheader, size); - } else { + if (version == 0) { + status = smbiosstore_v0(t, n, RecordName2); + } else if (version == 1) { + status = smbiosstore_v1(t, n, RecordName2); + } + } else { printf("This may be a mistake, string is null, please check !!! \n"); - goto done; - } + goto done; + } } else { - printf("This may be a mistake, string is null, please check !!! \n"); - goto done; - } - } else { printf("This may be a mistake, string is null, please check !!! \n"); - goto done; + goto done; + } + } else { + printf("This may be a mistake, string is null, please check !!! \n"); + goto done; } - - free(smbiosheader); - printf("Program OK.\n"); done: status = releaseMem(p); close(fd); diff --git a/LoongArch/spi.c b/LoongArch/spi.c index c6ce76b..46a9811 100644 --- a/LoongArch/spi.c +++ b/LoongArch/spi.c @@ -1015,7 +1015,7 @@ static int spi_read_flash (const char* addr, int count) return status; } -int spi_update_smbios(const char *addr, int command, const char **arg); +int spi_update_smbios(const char *addr, unsigned int maxsize, int command, const char **arg); static const char *const spi_usages[] = { PROGRAM_NAME" spi ", @@ -1124,7 +1124,7 @@ int cmd_spi (int argc, const char **argv) } spi_update_gmac(addr, id, mac); } else if (flag_smbios) { - spi_update_smbios(addr,command,argv); + spi_update_smbios(addr,count,command,argv); } else if (flag_write) { spi_update_region(addr, offset, data); } diff --git a/Script/smbios/smbiosstore.sh b/Script/smbios/smbiosstore.sh new file mode 100755 index 0000000..58300f2 --- /dev/null +++ b/Script/smbios/smbiosstore.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +./OsTools spi -C -s 1 1 type1_index1_Manufacture # update smbios type1 Manufacture +./OsTools spi -C -s 1 2 type1_index2_ProductName # update smbios type1 ProductName +./OsTools spi -C -s 1 3 type1_index3_Version # update smbios type1 Version +./OsTools spi -C -s 1 4 type1_index4_SerialNumber # update smbios type1 SerialNumber +./OsTools spi -C -s 1 5 type1_index5_SKUNumber # update smbios type1 SKUNumber +./OsTools spi -C -s 1 6 type1_index6_Family # update smbios type1 Family +./OsTools spi -C -s 1 7 00112233-4455-6677-8899-AABBCCDDEEFF # update smbios type1 UUID +./OsTools spi -C -s 2 1 type2_index1_Manufacture # update smbios type2 Manufacture +./OsTools spi -C -s 2 2 type2_index2_Product # update smbios type2 Product +./OsTools spi -C -s 2 3 type2_index3_Version # update smbios type2 Version +./OsTools spi -C -s 2 4 type2_index4_SerialNumber # update smbios type2 SerialNumber +./OsTools spi -C -s 2 5 type2_index5_AssetTag # update smbios type2 AssetTag +./OsTools spi -C -s 3 1 type3_index1_Manufacture # update smbios type3 Manufacture +./OsTools spi -C -s 3 2 type3_index2_Version # update smbios type3 Version +./OsTools spi -C -s 3 3 type3_index3_SerialNumber # update smbios type3 SerialNumber +./OsTools spi -C -s 3 4 type3_index4_AssetTag # update smbios type3 AssetTag +./OsTools spi -C -s 3 5 type3_index5_SKUNumber # update smbios type3 SKUNumber + +# demo with '-c' +# ./OsTools spi -c 0x4000 -C -s 1 1 type1_index1_Manufacture +# ./OsTools spi -c 0x4000 -C -s 1 2 type1_index2_ProductName +# ./OsTools spi -c 0x4000 -C -s 1 3 type1_index3_Version +# ./OsTools spi -c 0x4000 -C -s 1 4 type1_index4_SerialNumber +# ./OsTools spi -c 0x4000 -C -s 1 5 type1_index5_SKUNumber +# ./OsTools spi -c 0x4000 -C -s 1 6 type1_index6_Family +# ./OsTools spi -c 0x4000 -C -s 1 7 00112233-4455-6677-8899-AABBCCDDEEFF +# ./OsTools spi -c 0x4000 -C -s 2 1 type2_index1_Manufacture +# ./OsTools spi -c 0x4000 -C -s 2 2 type2_index2_Product +# ./OsTools spi -c 0x4000 -C -s 2 3 type2_index3_Version +# ./OsTools spi -c 0x4000 -C -s 2 4 type2_index4_SerialNumber +# ./OsTools spi -c 0x4000 -C -s 2 5 type2_index5_AssetTag +# ./OsTools spi -c 0x4000 -C -s 3 1 type3_index1_Manufacture +# ./OsTools spi -c 0x4000 -C -s 3 2 type3_index2_Version +# ./OsTools spi -c 0x4000 -C -s 3 3 type3_index3_SerialNumber +# ./OsTools spi -c 0x4000 -C -s 3 4 type3_index4_AssetTag +# ./OsTools spi -c 0x4000 -C -s 3 5 type3_index5_SKUNumber \ No newline at end of file diff --git a/Script/smbios/smbiosstore7a.sh b/Script/smbios/smbiosstore7a.sh new file mode 100755 index 0000000..49bdea6 --- /dev/null +++ b/Script/smbios/smbiosstore7a.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +PCIE_DEV22_PATH="/sys/bus/pci/devices/0000:00:16.0" +get_pcie_bar0_base() +{ + local dev_path="$1" + if [ ! -f "${dev_path}/resource" ];then + echo "ERROR: ${dev_path}/resource not found!" >&2 + exit 1 + fi + # first line of resource is BAR0, get first field + cat "${dev_path}/resource" | head -n1 | cut -d' ' -f1 +} +bar_base=$(get_pcie_bar0_base "$PCIE_DEV22_PATH") + +./OsTools spi -a $bar_base -C -s 1 1 type1_index1_7a_Manufacture # update smbios type1 Manufacture +./OsTools spi -a $bar_base -C -s 1 2 type1_index2_7a_ProductName # update smbios type1 ProductName +./OsTools spi -a $bar_base -C -s 1 3 type1_index3_7a_Version # update smbios type1 Version +./OsTools spi -a $bar_base -C -s 1 4 type1_index4_7a_SerialNumber # update smbios type1 SerialNumber +./OsTools spi -a $bar_base -C -s 1 5 type1_index5_7a_SKUNumber # update smbios type1 SKUNumber +./OsTools spi -a $bar_base -C -s 1 6 type1_index6_7a_Family # update smbios type1 Family +./OsTools spi -a $bar_base -C -s 1 7 00112233-4455-6677-8899-AABBCCDDEEFF # update smbios type1 UUID +./OsTools spi -a $bar_base -C -s 2 1 type2_index1_7a_Manufacture # update smbios type2 Manufacture +./OsTools spi -a $bar_base -C -s 2 2 type2_index2_7a_Product # update smbios type2 Product +./OsTools spi -a $bar_base -C -s 2 3 type2_index3_7a_Version # update smbios type2 Version +./OsTools spi -a $bar_base -C -s 2 4 type2_index4_7a_SerialNumber # update smbios type2 SerialNumber +./OsTools spi -a $bar_base -C -s 2 5 type2_index5_7a_AssetTag # update smbios type2 AssetTag +./OsTools spi -a $bar_base -C -s 3 1 type3_index1_7a_Manufacture # update smbios type3 Manufacture +./OsTools spi -a $bar_base -C -s 3 2 type3_index2_7a_Version # update smbios type3 Version +./OsTools spi -a $bar_base -C -s 3 3 type3_index3_7a_SerialNumber # update smbios type3 SerialNumber +./OsTools spi -a $bar_base -C -s 3 4 type3_index4_7a_AssetTag # update smbios type3 AssetTag +./OsTools spi -a $bar_base -C -s 3 5 type3_index5_7a_SKUNumber # update smbios type3 SKUNumber diff --git a/Script/smbios/smbiosupdate.nsh b/Script/smbios/smbiosupdate.nsh new file mode 100755 index 0000000..5b5305e --- /dev/null +++ b/Script/smbios/smbiosupdate.nsh @@ -0,0 +1,36 @@ +smbiosupdate -w -t 1 -n 1 -s type1_index1_Manufacture +smbiosupdate -w -t 1 -n 2 -s type1_index2_ProductName +smbiosupdate -w -t 1 -n 3 -s type1_index3_Version +smbiosupdate -w -t 1 -n 4 -s type1_index4_SerialNumber +smbiosupdate -w -t 1 -n 5 -s type1_index5_SKUNumber +smbiosupdate -w -t 1 -n 6 -s type1_index6_Family +smbiosupdate -w -t 1 -n 7 -s 00112233-4455-6677-8899-AABBCCDDEEFF +smbiosupdate -w -t 2 -n 1 -s type2_index1_Manufacture +smbiosupdate -w -t 2 -n 2 -s type2_index2_Product +smbiosupdate -w -t 2 -n 3 -s type2_index3_Version +smbiosupdate -w -t 2 -n 4 -s type2_index4_SerialNumber +smbiosupdate -w -t 2 -n 5 -s type2_index5_AssetTag +smbiosupdate -w -t 3 -n 1 -s type3_index1_Manufacture +smbiosupdate -w -t 3 -n 2 -s type3_index2_Version +smbiosupdate -w -t 3 -n 3 -s type3_index3_SerialNumber +smbiosupdate -w -t 3 -n 4 -s type3_index4_AssetTag +smbiosupdate -w -t 3 -n 5 -s type3_index5_SKUNumber + +# Demo script to store information in 7A flash +# smbiosupdate -7a -w -t 1 -n 1 -s type1_index1_Manufacture +# smbiosupdate -7a -w -t 1 -n 2 -s type1_index2_ProductName +# smbiosupdate -7a -w -t 1 -n 3 -s type1_index3_Version +# smbiosupdate -7a -w -t 1 -n 4 -s type1_index4_SerialNumber +# smbiosupdate -7a -w -t 1 -n 5 -s type1_index5_SKUNumber +# smbiosupdate -7a -w -t 1 -n 6 -s type1_index6_Family +# smbiosupdate -7a -w -t 1 -n 7 -s 00112233-4455-6677-8899-AABBCCDDEEFF +# smbiosupdate -7a -w -t 2 -n 1 -s type2_index1_Manufacture +# smbiosupdate -7a -w -t 2 -n 2 -s type2_index2_Product +# smbiosupdate -7a -w -t 2 -n 3 -s type2_index3_Version +# smbiosupdate -7a -w -t 2 -n 4 -s type2_index4_SerialNumber +# smbiosupdate -7a -w -t 2 -n 5 -s type2_index5_AssetTag +# smbiosupdate -7a -w -t 3 -n 1 -s type3_index1_Manufacture +# smbiosupdate -7a -w -t 3 -n 2 -s type3_index2_Version +# smbiosupdate -7a -w -t 3 -n 3 -s type3_index3_SerialNumber +# smbiosupdate -7a -w -t 3 -n 4 -s type3_index4_AssetTag +# smbiosupdate -7a -w -t 3 -n 5 -s type3_index5_SKUNumber