This one's for Ron.

On Fri, Oct 23, 2015 at 10:32 AM, ron minnich <rminn...@gmail.com> wrote:
> Build the tool in go. It's trivial. If you have an idea how it ought to work
> I can set it up in the playground in a few minutes.
>
> ron
>
> On Fri, Oct 23, 2015 at 8:24 AM Patrick Georgi <pgeo...@google.com> wrote:
>>
>> Hi,
>>
>> Some mainboards come with soldered-on memory without SPD ROM. For
>> these, we carry the SPD data in coreboot.
>>
>> Currently, they're stored in a hexdump format that is then converted
>> to binary at build time. The various mechanisms of doing so fail on
>> some platform or another:
>>  - "echo -e -n $stuff" isn't well-liked by some shells (emits "e -n
>> $stuff")
>>  - "printf '\x42'" isn't well-liked by some shells (or /usr/bin/printf
>> tools) that don't support hexadecimal formats
>>  - "printf '\0377'" isn't well-liked by some non-conforming, but existing
>> shells
>>  - xxd -rg1 $file > $file.bin requires xxd, which comes with vim and
>> may just not exist
>>
>> I see essentially two ways out of this, before we can start reducing
>> duplication across the tree in that area:
>> We could build our own tool to parse hex files and dump binary, or we
>> could ship SPD data as binary from the start (and only have to
>> concatenate them).
>>
>> The second option has the appeal of being much simpler (and there
>> isn't really a "preferred form" for editing SPD data that I'm aware of
>> - is there?), but looks icky at a glance because it's binary (but it's
>> really just as impenetrable as the equivalent hexdump).
>>
>> So what do these files contain? Parameters (as in: facts) about the
>> hardware's size, layout, and timing, and a bunch of vendor/model
>> identifier strings.
>>
>>
>> So, is there a third option that I'm missing? Other opinions?
>> Patrick
>> --
>> Google Germany GmbH, ABC-Str. 19, 20354 Hamburg
>> Registergericht und -nummer: Hamburg, HRB 86891, Sitz der Gesellschaft:
>> Hamburg
>> Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
>>
>> --
>> coreboot mailing list: coreboot@coreboot.org
>> http://www.coreboot.org/mailman/listinfo/coreboot
>
>
> --
> coreboot mailing list: coreboot@coreboot.org
> http://www.coreboot.org/mailman/listinfo/coreboot
package main

import (
	"bufio"
	"bytes"
	"encoding/hex"
	"flag"
	"log"
	"os"
	"path/filepath"
	"strings"
)

var fileName = flag.String("ifilename", "", "input file to parse")

func readSpd(filename string) ([]byte, error) {

	f, err := os.Open(filename)

	if err != nil {
		return nil, err
	}

	data := make([]byte, 0)

	s := bufio.NewScanner(f)
	s.Split(bufio.ScanLines)
	for s.Scan() {
		hex := s.Bytes()
		if !bytes.Contains(hex, []byte{'#'}) {
			data = append(data, hex...)
		}
	}

	s = bufio.NewScanner(bytes.NewBuffer(data))
	s.Split(bufio.ScanWords)

	data = []byte{}

	for s.Scan() {
		data = append(data, s.Bytes()...)
	}

	defer f.Close()

	return data, nil
}

func binFileName(name string) string {
	dir := filepath.Dir(name)
	base := filepath.Base(name)

	ext := filepath.Ext(base)
	if ext != "" {
		upto := len(base) - len(ext)
		asSlice := []byte(base)
		base = string(asSlice[:upto])
	}

	return filepath.Join(dir, strings.Join([]string{base, ".bin"}, ""))
}

func main() {
	flag.Parse()

	if *fileName == "" {
		log.Fatal("Need to supply input file.")
	}

	data, err := readSpd(*fileName)

	if err != nil {
		log.Fatalf("File parsing failed. %s\n", err)
	}

	log.Printf("%v\n", data)

	bdata := make([]byte, hex.DecodedLen(len(data)))

	_, err = hex.Decode(bdata, data)

	if err != nil {
		log.Fatalf("Couldn't convert file to binary: %s\n", err)
	}

	f, err := os.Create(binFileName(*fileName))

	if err != nil {
		log.Fatalf("Unable to create binary file: %s\n", err)
	}

	_, err = f.Write(bdata)

	f.Close()

	if err != nil {
		os.Remove(f.Name())
		log.Fatal("Unable to write data to file: %s\n", err)
	}
}
-- 
coreboot mailing list: coreboot@coreboot.org
http://www.coreboot.org/mailman/listinfo/coreboot

Reply via email to