func Join()

in imfg/imfg.go [177:228]


func Join(mm NameBlobMap, eraseVal byte,
	areas []flash.FlashArea) ([]byte, error) {

	// Keep track of which areas we haven't seen yet.
	unseen := map[string]struct{}{}
	for name, _ := range mm {
		unseen[name] = struct{}{}
	}

	joined := []byte{}
	for _, area := range areas {
		bin := mm[area.Name]

		// Only include this area if it belongs to the mfg image we are
		// joining.
		if bin != nil {
			delete(unseen, area.Name)

			// Pad remainder of previous area in this section.
			padSize := area.Offset - len(joined)
			if padSize > 0 {
				joined = mfg.AddPadding(joined, eraseVal, padSize)
			}

			// Append data to joined binary.
			binstr := ""
			if len(bin) >= 4 {
				binstr = fmt.Sprintf("%x", bin[:4])
			}
			iutil.Printf("inserting %s (%s) at offset %d (0x%x)\n",
				area.Name, binstr, len(joined), len(joined))
			joined = append(joined, bin...)
		}
	}

	// Ensure we processed every area in the map.
	if len(unseen) > 0 {
		names := []string{}
		for name, _ := range unseen {
			names = append(names, name)
		}
		sort.Strings(names)

		return nil, errors.Errorf(
			"unprocessed flash areas: %s", strings.Join(names, ", "))
	}

	// Strip padding from the end of the joined binary.
	joined = StripPadding(joined, eraseVal)

	return joined, nil
}