func runRmtlvsMfgCmd()

in cli/mfg_cmds.go [506:579]


func runRmtlvsMfgCmd(cmd *cobra.Command, args []string) {
	if len(args) < 2 {
		ImgmodUsage(cmd, nil)
	}

	mfgDir := args[0]

	outFilename, err := CalcOutFilename(
		mfgDir + "/" + mfg.MFG_BIN_IMG_FILENAME)
	if err != nil {
		ImgmodUsage(cmd, err)
	}

	m, man, err := readMfgDir(mfgDir)
	if err != nil {
		ImgmodUsage(cmd, err)
	}

	numTlvs := 0
	if m.Meta != nil {
		numTlvs = len(m.Meta.Tlvs)
	}

	tlvIndices := []int{}
	idxMap := map[int]struct{}{}
	for _, arg := range args[1:] {
		idx, err := strconv.Atoi(arg)
		if err != nil {
			ImgmodUsage(cmd, errors.Errorf("invalid TLV index: %s", arg))
		}

		if idx < 0 || idx >= numTlvs {
			ImgmodUsage(nil, errors.Errorf(
				"TLV index %s out of range; "+
					"must be in range [0, %d] for this mfgimage",
				arg, numTlvs-1))
		}

		if _, ok := idxMap[idx]; ok {
			ImgmodUsage(nil, errors.Errorf(
				"TLV index %d specified more than once", idx))
		}
		idxMap[idx] = struct{}{}

		tlvIndices = append(tlvIndices, idx)
	}

	// Remove TLVs in reverse order to preserve index mapping.
	sort.Sort(sort.Reverse(sort.IntSlice(tlvIndices)))
	for _, idx := range tlvIndices {
		tlv := m.Meta.Tlvs[idx]
		iutil.Printf("Removing TLV%d: %s\n", idx, mfgTlvStr(tlv))

		tlvSz := mfg.META_TLV_HEADER_SZ + len(tlv.Data)
		m.MetaOff += tlvSz
		m.Meta.Footer.Size -= uint16(tlvSz)

		m.Meta.Tlvs = append(m.Meta.Tlvs[0:idx], m.Meta.Tlvs[idx+1:]...)
	}

	// Rehash.
	if err := m.RefillHash(man.EraseVal); err != nil {
		ImgmodUsage(nil, err)
	}

	// Write new artifacts.
	newBin, err := m.Bytes(man.EraseVal)
	if err != nil {
		ImgmodUsage(nil, err)
	}
	if err := WriteFile(newBin, outFilename); err != nil {
		ImgmodUsage(nil, err)
	}
}