// @return                      [paths-of-artifacts], error
func (mi *MfgImage) CreateMfgImage() ([]string, error) {
	cs, err := mi.build()
	if err != nil {
		return nil, err
	}

	sectionDir := MfgSectionBinDir(mi.basePkg.Name())
	if err := os.MkdirAll(sectionDir, 0755); err != nil {
		return nil, util.ChildNewtError(err)
	}

	for device, section := range cs.dsMap {
		sectionPath := MfgSectionBinPath(mi.basePkg.Name(), device)
		if err := ioutil.WriteFile(sectionPath, section, 0644); err != nil {
			return nil, util.ChildNewtError(err)
		}
	}

	manifest, err := mi.createManifest(cs)
	if err != nil {
		return nil, err
	}

	manifestPath := mi.ManifestPath()
	if err := ioutil.WriteFile(manifestPath, manifest, 0644); err != nil {
		return nil, util.FmtNewtError("Failed to write mfg manifest file: %s",
			err.Error())
	}

	return mi.ToPaths(), nil
}
Exemple #2
0
func writeElem(elem interface{}, buf *bytes.Buffer) error {
	/* XXX: Assume target platform uses little endian. */
	if err := binary.Write(buf, binary.LittleEndian, elem); err != nil {
		return util.ChildNewtError(err)
	}
	return nil
}
func (mi *MfgImage) copyBinFiles() error {
	dstPath := MfgBinDir(mi.basePkg.Name())
	if err := os.MkdirAll(filepath.Dir(dstPath), 0755); err != nil {
		return util.ChildNewtError(err)
	}

	bootPaths := bootLoaderFromPaths(mi.boot)
	for _, path := range bootPaths {
		dstDir := MfgBootDir(mi.basePkg.Name())
		if err := mi.copyBinFile(path, dstDir); err != nil {
			return err
		}
	}

	for i, imgTarget := range mi.images {
		imgPaths := imageFromPaths(imgTarget)
		dstDir := MfgImageBinDir(mi.basePkg.Name(), i)
		for _, path := range imgPaths {
			if err := mi.copyBinFile(path, dstDir); err != nil {
				return err
			}
		}
	}

	return nil
}
func (pw *PackageWriter) fixupPKG() error {
	pkgBase := path.Base(pw.fullName)

	// Move include file to name after package name
	if err := util.MoveFile(pw.targetPath+"/include/your-path/your-file.h",
		pw.targetPath+"/include/your-path/"+pkgBase+".h"); err != nil {
		return err
	}

	// Move source file
	if err := util.MoveFile(pw.targetPath+"/src/your-source.c",
		pw.targetPath+"/src/"+pkgBase+".c"); err != nil {
		return err
	}

	if err := util.CopyDir(pw.targetPath+"/include/your-path/",
		pw.targetPath+"/include/"+pkgBase+"/"); err != nil {
		return err
	}

	if err := os.RemoveAll(pw.targetPath + "/include/your-path/"); err != nil {
		return util.ChildNewtError(err)
	}

	if err := pw.cleanupPackageFile(pw.targetPath + "/pkg.yml"); err != nil {
		return err
	}

	return nil
}
func (pw *PackageWriter) cleanupPackageFile(pfile string) error {
	f, err := os.Open(pfile)
	if err != nil {
		return util.ChildNewtError(err)
	}
	defer f.Close()

	data, _ := ioutil.ReadAll(f)

	// Search & replace file contents
	re := regexp.MustCompile("your-pkg-name")
	res := re.ReplaceAllString(string(data), pw.fullName)

	if err := ioutil.WriteFile(pfile, []byte(res), 0666); err != nil {
		return util.ChildNewtError(err)
	}

	return nil
}
func readManifest(path string) (*image.ImageManifest, error) {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, util.ChildNewtError(err)
	}

	manifest := &image.ImageManifest{}
	if err := json.Unmarshal(content, &manifest); err != nil {
		return nil, util.FmtNewtError(
			"Failure decoding manifest with path \"%s\": %s", err.Error())
	}

	return manifest, nil
}
func (mi *MfgImage) partFromImage(
	imgPath string, flashAreaName string) (mfgPart, error) {

	part := mfgPart{
		// Boot loader and images always go in device 0.
		device: 0,
	}

	area, ok := mi.bsp.FlashMap.Areas[flashAreaName]
	if !ok {
		return part, util.FmtNewtError(
			"Image at \"%s\" requires undefined flash area \"%s\"",
			imgPath, flashAreaName)
	}

	part.name = fmt.Sprintf("%s (%s)", flashAreaName, filepath.Base(imgPath))
	part.offset = area.Offset

	var err error

	part.data, err = ioutil.ReadFile(imgPath)
	if err != nil {
		return part, util.ChildNewtError(err)
	}

	overflow := len(part.data) - area.Size
	if overflow > 0 {
		return part, util.FmtNewtError(
			"Image \"%s\" is too large to fit in flash area \"%s\"; "+
				"image-size=%d flash-area-size=%d overflow=%d",
			imgPath, flashAreaName, len(part.data), area.Size, overflow)
	}

	// If an image slot is used, the entire flash area is unwritable.  This
	// restriction comes from the boot loader's need to write status at the end
	// of an area.  Pad out part with unwriten flash (0xff).  This probably
	// isn't terribly efficient...
	for i := 0; i < -overflow; i++ {
		part.data = append(part.data, 0xff)
	}

	return part, nil
}
func (cs *ConnSerial) Open(cp config.NewtmgrConnProfile, readTimeout time.Duration) error {

	c, err := newSerialConfig(cp.ConnString(), readTimeout)
	if err != nil {
		return err
	}

	cs.serialChannel, err = serial.OpenPort(c)
	if err != nil {
		return util.ChildNewtError(err)
	}
	//defer cs.serialChannel.Close()

	// Most of the reading will be done line by line, use the
	// bufio.Scanner to do this
	cs.scanner = bufio.NewScanner(cs.serialChannel)

	return nil
}
func newSerialConfig(
	connString string, readTimeout time.Duration) (*serial.Config, error) {

	fields := strings.Split(connString, ":")
	if len(fields) == 0 {
		return nil, util.FmtNewtError("invalid connstring: %s", connString)
	}

	name := ""
	baud := 115200

	for _, field := range fields {
		parts := strings.Split(field, "=")
		if len(parts) == 2 {
			if parts[0] == "baud" {
				var err error
				baud, err = strconv.Atoi(parts[1])
				if err != nil {
					return nil, util.ChildNewtError(err)
				}

			}

			if parts[0] == "dev" {
				name = parts[1]
			}
		}
	}

	// Handle old-style conn string (single token indicating dev file).
	if name == "" {
		name = fields[0]
	}

	c := &serial.Config{
		Name:        name,
		Baud:        baud,
		ReadTimeout: readTimeout,
	}

	return c, nil
}