github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/exp/dmidecode/dmidecode.go (about) 1 // Copyright 2016-2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "fmt" 9 "io" 10 "os" 11 "strconv" 12 "strings" 13 14 flag "github.com/spf13/pflag" 15 16 "github.com/mvdan/u-root-coreutils/pkg/smbios" 17 ) 18 19 var ( 20 flagDumpBin = flag.String("dump-bin", "", `Do not decode the entries, instead dump the DMI data to a file in binary form. The generated file is suitable to pass to --from-dump later.`) 21 flagFromDump = flag.String("from-dump", "", `Read the DMI data from a binary file previously generated using --dump-bin.`) 22 flagType = flag.StringSliceP("type", "t", nil, `Only display the entries of type TYPE. TYPE can be either a DMI type number, or a comma-separated list of type numbers, or a keyword from the following list: bios, system, baseboard, chassis, processor, memory, cache, connector, slot. If this option is used more than once, the set of displayed entries will be the union of all the given types. If TYPE is not provided or not valid, a list of all valid keywords is printed and dmidecode exits with an error.`) 23 // NB: When adding flags, update resetFlags in dmidecode_test. 24 ) 25 26 var typeGroups = map[string][]uint8{ 27 "bios": {0, 13}, 28 "system": {1, 12, 15, 23, 32}, 29 "baseboard": {2, 10, 41}, 30 "chassis": {3}, 31 "processor": {4}, 32 "memory": {5, 6, 16, 17}, 33 "cache": {7}, 34 "connector": {8}, 35 "slot": {9}, 36 } 37 38 type dmiDecodeError struct { 39 error 40 code int 41 } 42 43 // parseTypeFilter parses the --type argument(s) and returns a set of types taht should be included. 44 func parseTypeFilter(typeStrings []string) (map[smbios.TableType]bool, error) { 45 types := map[smbios.TableType]bool{} 46 for _, ts := range typeStrings { 47 if tg, ok := typeGroups[strings.ToLower(ts)]; ok { 48 for _, t := range tg { 49 types[smbios.TableType(t)] = true 50 } 51 } else { 52 u, err := strconv.ParseUint(ts, 0, 8) 53 if err != nil { 54 return nil, fmt.Errorf("Invalid type: %s", ts) 55 } 56 types[smbios.TableType(uint8(u))] = true 57 } 58 } 59 return types, nil 60 } 61 62 func dumpBin(textOut io.Writer, entryData, tableData []byte, fileName string) *dmiDecodeError { 63 // Need to rewrite address to be compatible with dmidecode(8). 64 e32, e64, err := smbios.ParseEntry(entryData) 65 if err != nil { 66 return &dmiDecodeError{code: 1, error: fmt.Errorf("error parsing entry point structure: %v", err)} 67 } 68 var edata []byte 69 switch { 70 case e32 != nil: 71 e32.StructTableAddr = 0x20 72 edata, _ = e32.MarshalBinary() 73 case e64 != nil: 74 e64.StructTableAddr = 0x20 75 edata, _ = e64.MarshalBinary() 76 } 77 f, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0o644) 78 if err != nil { 79 return &dmiDecodeError{code: 1, error: fmt.Errorf("error opening file for writing: %v", err)} 80 } 81 defer f.Close() 82 fmt.Fprintf(textOut, "# Writing %d bytes to %s.\n", len(edata), fileName) 83 if _, err := f.Write(edata); err != nil { 84 return &dmiDecodeError{code: 1, error: fmt.Errorf("error writing entry: %v", err)} 85 } 86 for i := len(edata); i < 0x20; i++ { 87 if _, err := f.Write([]byte{0}); err != nil { 88 return &dmiDecodeError{code: 1, error: fmt.Errorf("error writing entry: %v", err)} 89 } 90 } 91 fmt.Fprintf(textOut, "# Writing %d bytes to %s.\n", len(tableData), fileName) 92 if _, err := f.Write(tableData); err != nil { 93 return &dmiDecodeError{code: 1, error: fmt.Errorf("error writing table data: %v", err)} 94 } 95 return nil 96 } 97 98 func dmiDecode(textOut io.Writer) *dmiDecodeError { 99 typeFilter, err := parseTypeFilter(*flagType) 100 if err != nil { 101 return &dmiDecodeError{code: 2, error: fmt.Errorf("invalid --type: %v", err)} 102 } 103 fmt.Fprintf(textOut, "# dmidecode-go\n") // TODO: version. 104 entryData, tableData, err := getData(textOut, *flagFromDump, "/sys/firmware/dmi/tables") 105 if err != nil { 106 return &dmiDecodeError{code: 1, error: fmt.Errorf("error parsing loading data: %v", err)} 107 } 108 if *flagDumpBin != "" { 109 return dumpBin(textOut, entryData, tableData, *flagDumpBin) 110 } 111 si, err := smbios.ParseInfo(entryData, tableData) 112 if err != nil { 113 return &dmiDecodeError{code: 1, error: fmt.Errorf("error parsing data: %v", err)} 114 } 115 if si.Entry64 != nil { 116 fmt.Fprintf(textOut, "SMBIOS %d.%d.%d present.\n", si.MajorVersion(), si.MinorVersion(), si.DocRev()) 117 } else { 118 fmt.Fprintf(textOut, "SMBIOS %d.%d present.\n", si.MajorVersion(), si.MinorVersion()) 119 } 120 if si.Entry32 != nil { 121 fmt.Fprintf(textOut, "%d structures occupying %d bytes.\n", si.Entry32.NumberOfStructs, si.Entry32.StructTableLength) 122 } 123 fmt.Fprintf(textOut, "\n") 124 for _, t := range si.Tables { 125 if len(typeFilter) != 0 && !typeFilter[t.Type] { 126 continue 127 } 128 pt, err := smbios.ParseTypedTable(t) 129 if err != nil { 130 if err != smbios.ErrUnsupportedTableType { 131 fmt.Fprintf(os.Stderr, "%s\n", err) 132 } 133 // Print as raw table 134 pt = t 135 } 136 fmt.Fprintf(textOut, "%s\n\n", pt) 137 } 138 return nil 139 } 140 141 func main() { 142 flag.Parse() 143 err := dmiDecode(os.Stdout) 144 if err != nil { 145 fmt.Fprintf(os.Stderr, "%s\n", err) 146 os.Exit(err.code) 147 } 148 }