github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/smbios/smbios_linux.go (about)

     1  // Copyright 2016-2021 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 smbios
     6  
     7  import (
     8  	"bufio"
     9  	"fmt"
    10  	"log"
    11  	"os"
    12  	"strconv"
    13  	"strings"
    14  )
    15  
    16  var systabPath = "/sys/firmware/efi/systab"
    17  
    18  // SMBIOSBaseEFI finds the SMBIOS entry point address in the EFI System Table.
    19  func SMBIOSBaseEFI() (base int64, size int64, err error) {
    20  	file, err := os.Open(systabPath)
    21  	if err != nil {
    22  		return 0, 0, err
    23  	}
    24  	defer file.Close()
    25  
    26  	const (
    27  		smbios3 = "SMBIOS3="
    28  		smbios  = "SMBIOS="
    29  	)
    30  
    31  	scanner := bufio.NewScanner(file)
    32  	for scanner.Scan() {
    33  		line := scanner.Text()
    34  		start := ""
    35  		size := int64(0)
    36  		if strings.HasPrefix(line, smbios3) {
    37  			start = strings.TrimPrefix(line, smbios3)
    38  			size = smbios3HeaderSize
    39  		}
    40  		if strings.HasPrefix(line, smbios) {
    41  			start = strings.TrimPrefix(line, smbios)
    42  			size = smbios2HeaderSize
    43  		}
    44  		if start == "" {
    45  			continue
    46  		}
    47  		base, err := strconv.ParseInt(start, 0, 63)
    48  		if err != nil {
    49  			continue
    50  		}
    51  		return base, size, nil
    52  	}
    53  	if err := scanner.Err(); err != nil {
    54  		log.Printf("error while reading EFI systab: %v", err)
    55  	}
    56  	return 0, 0, fmt.Errorf("invalid /sys/firmware/efi/systab file")
    57  }