You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/05/20 00:44:01 UTC

[1/2] incubator-mynewt-newt git commit: newtmgr; commands to download/list/erase coredump. Code to convert downloaded dump into a elf corefile.

Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/develop 3960627ba -> ff51f0731


newtmgr; commands to download/list/erase coredump.
Code to convert downloaded dump into a elf corefile.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/de7ece74
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/de7ece74
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/de7ece74

Branch: refs/heads/develop
Commit: de7ece74cd385101eab1cff866fd66438d0acfb3
Parents: 3960627
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu May 19 17:34:39 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Thu May 19 17:34:39 2016 -0700

----------------------------------------------------------------------
 newtmgr/cli/image.go          | 260 +++++++++++++++++++++++++++++--
 newtmgr/core/core_convert.go  | 304 +++++++++++++++++++++++++++++++++++++
 newtmgr/protocol/coreerase.go |  63 ++++++++
 newtmgr/protocol/corelist.go  |  63 ++++++++
 newtmgr/protocol/coreload.go  | 131 ++++++++++++++++
 newtmgr/protocol/imagelist.go |  10 +-
 newtmgr/protocol/nmgr.go      |   9 ++
 7 files changed, 824 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/de7ece74/newtmgr/cli/image.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/image.go b/newtmgr/cli/image.go
index 8f4f52b..393dd91 100644
--- a/newtmgr/cli/image.go
+++ b/newtmgr/cli/image.go
@@ -26,9 +26,11 @@ import (
 	"os"
 
 	"mynewt.apache.org/newt/newtmgr/config"
+	"mynewt.apache.org/newt/newtmgr/core"
 	"mynewt.apache.org/newt/newtmgr/protocol"
 	"mynewt.apache.org/newt/newtmgr/transport"
 	"mynewt.apache.org/newt/util"
+
 	"github.com/spf13/cobra"
 )
 
@@ -396,6 +398,192 @@ func fileDownloadCmd(cmd *cobra.Command, args []string) {
 	fmt.Println("Done")
 }
 
+func coreDownloadCmd(cmd *cobra.Command, args []string) {
+	if len(args) < 1 {
+		fmt.Println("Need to specify target filename to download")
+		return
+	}
+
+	cpm, err := config.NewConnProfileMgr()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	profile, err := cpm.GetConnProfile(ConnProfileName)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	conn, err := transport.NewConn(profile)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	runner, err := protocol.NewCmdRunner(conn)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	tmpName := args[0] + ".tmp"
+	file, err := os.OpenFile(tmpName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
+	if err != nil {
+		nmUsage(cmd, util.NewNewtError(fmt.Sprintf(
+			"Cannot open file %s - %s", tmpName, err.Error())))
+	}
+
+	coreDownload, err := protocol.NewCoreDownload()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+	coreDownload.Runner = runner
+	coreDownload.File = file
+
+	err = coreDownload.Download()
+	file.Close()
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+
+	fmt.Println("Coredump download completed")
+
+	/*
+	 * Download finished. Now convert to ELF corefile format.
+	 */
+	coreConvert := core.NewCoreConvert()
+
+	file, err = os.OpenFile(tmpName, os.O_RDONLY, 0)
+	if err != nil {
+		nmUsage(cmd, util.NewNewtError(fmt.Sprintf(
+			"Cannot open file %s - %s", tmpName, err.Error())))
+	}
+
+	coreConvert.Source = file
+
+	file, err = os.OpenFile(args[0], os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
+	if err != nil {
+		nmUsage(cmd, util.NewNewtError(fmt.Sprintf(
+			"Cannot open file %s - %s", args[0], err.Error())))
+	}
+	coreConvert.Target = file
+
+	err = coreConvert.Convert()
+
+	coreConvert.Source.Close()
+	coreConvert.Target.Close()
+	os.Remove(tmpName)
+
+	if err != nil {
+		fmt.Println(err)
+	} else {
+		fmt.Printf("Corefile created for\n   %x\n", coreConvert.ImageHash)
+	}
+}
+
+func coreListCmd(cmd *cobra.Command, args []string) {
+	cpm, err := config.NewConnProfileMgr()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	profile, err := cpm.GetConnProfile(ConnProfileName)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	conn, err := transport.NewConn(profile)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	runner, err := protocol.NewCmdRunner(conn)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	coreList, err := protocol.NewCoreList()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	nmr, err := coreList.EncodeWriteRequest()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	if err := runner.WriteReq(nmr); err != nil {
+		nmUsage(cmd, err)
+	}
+
+	rsp, err := runner.ReadResp()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	clRsp, err := protocol.DecodeCoreListResponse(rsp.Data)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+	if clRsp.ErrCode == protocol.NMGR_ERR_OK {
+		fmt.Printf("Corefile present\n")
+	} else if clRsp.ErrCode == protocol.NMGR_ERR_ENOENT {
+		fmt.Printf("No corefiles\n")
+	} else {
+		fmt.Printf("List failed: %d\n", clRsp.ErrCode)
+	}
+}
+
+func coreEraseCmd(cmd *cobra.Command, args []string) {
+	cpm, err := config.NewConnProfileMgr()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	profile, err := cpm.GetConnProfile(ConnProfileName)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	conn, err := transport.NewConn(profile)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	runner, err := protocol.NewCmdRunner(conn)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	coreErase, err := protocol.NewCoreErase()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	nmr, err := coreErase.EncodeWriteRequest()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	if err := runner.WriteReq(nmr); err != nil {
+		nmUsage(cmd, err)
+	}
+
+	rsp, err := runner.ReadResp()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	ceRsp, err := protocol.DecodeCoreEraseResponse(rsp.Data)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+	if ceRsp.ErrCode != 0 {
+		fmt.Printf("Erase failed: %d\n", ceRsp.ErrCode)
+	} else {
+		fmt.Printf("Done\n")
+	}
+}
+
 func imageCmd() *cobra.Command {
 	imageCmd := &cobra.Command{
 		Use:   "image",
@@ -412,33 +600,81 @@ func imageCmd() *cobra.Command {
 	}
 	imageCmd.AddCommand(listCmd)
 
+	uploadEx := "  newtmgr -c olimex image upload <image_file\n"
+	uploadEx += "  newtmgr -c olimex image upload bin/slinky_zero/apps/slinky.img\n"
+
 	uploadCmd := &cobra.Command{
-		Use:   "upload",
-		Short: "Upload image to target",
-		Run:   imageUploadCmd,
+		Use:     "upload",
+		Short:   "Upload image to target",
+		Example: uploadEx,
+		Run:     imageUploadCmd,
 	}
 	imageCmd.AddCommand(uploadCmd)
 
+	bootEx := "  newtmgr -c olimex image boot [<version>]\n"
+	bootEx += "  newtmgr -c olimex image boot\n"
+	bootEx += "  newtmgr -c olimex image boot 1.2.3\n"
+
 	bootCmd := &cobra.Command{
-		Use:   "boot",
-		Short: "Which image to boot",
-		Run:   imageBootCmd,
+		Use:     "boot",
+		Short:   "Which image to boot",
+		Example: bootEx,
+		Run:     imageBootCmd,
 	}
 	imageCmd.AddCommand(bootCmd)
 
+	fileUploadEx := "  newtmgr -c olimex image fileupload <filename> <tgt_file>\n"
+	fileUploadEx += "  newtmgr -c olimex image fileupload sample.lua /sample.lua\n"
+
 	fileUploadCmd := &cobra.Command{
-		Use:   "fileupload",
-		Short: "Upload file to target",
-		Run:   fileUploadCmd,
+		Use:     "fileupload",
+		Short:   "Upload file to target",
+		Example: fileUploadEx,
+		Run:     fileUploadCmd,
 	}
 	imageCmd.AddCommand(fileUploadCmd)
 
+	fileDownloadEx := "  newtmgr -c olimex image filedownload <tgt_file> <filename>\n"
+	fileDownloadEx += "  newtmgr -c olimex image filedownload /cfg/mfg mfg.txt\n"
+
 	fileDownloadCmd := &cobra.Command{
-		Use:   "filedownload",
-		Short: "Download file from target",
-		Run:   fileDownloadCmd,
+		Use:     "filedownload",
+		Short:   "Download file from target",
+		Example: fileDownloadEx,
+		Run:     fileDownloadCmd,
 	}
 	imageCmd.AddCommand(fileDownloadCmd)
 
+	coreListEx := "  newtmgr -c olimex image corelist\n"
+
+	coreListCmd := &cobra.Command{
+		Use:     "corelist",
+		Short:   "List core(s) on target",
+		Example: coreListEx,
+		Run:     coreListCmd,
+	}
+	imageCmd.AddCommand(coreListCmd)
+
+	coreEx := "  newtmgr -c olimex image coredownload <filename>\n"
+	coreEx += "  newtmgr -c olimex image coredownload core\n"
+
+	coreDownloadCmd := &cobra.Command{
+		Use:     "coredownload",
+		Short:   "Download core from target",
+		Example: coreEx,
+		Run:     coreDownloadCmd,
+	}
+	imageCmd.AddCommand(coreDownloadCmd)
+
+	coreEraseEx := "  newtmgr -c olimex image coreerase\n"
+
+	coreEraseCmd := &cobra.Command{
+		Use:     "coreerase",
+		Short:   "Erase core on target",
+		Example: coreEraseEx,
+		Run:     coreEraseCmd,
+	}
+	imageCmd.AddCommand(coreEraseCmd)
+
 	return imageCmd
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/de7ece74/newtmgr/core/core_convert.go
----------------------------------------------------------------------
diff --git a/newtmgr/core/core_convert.go b/newtmgr/core/core_convert.go
new file mode 100644
index 0000000..fd195b6
--- /dev/null
+++ b/newtmgr/core/core_convert.go
@@ -0,0 +1,304 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package core
+
+import (
+	"bytes"
+	"debug/elf"
+	"encoding/binary"
+	"fmt"
+	"io"
+	"os"
+
+	"mynewt.apache.org/newt/util"
+)
+
+type CoreConvert struct {
+	Source    *os.File
+	Target    *os.File
+	ImageHash []byte
+	elfHdr    *elf.Header32
+	phdrs     []*elf.Prog32
+	data      [][]byte
+}
+
+const (
+	COREDUMP_TLV_IMAGE = 1
+	COREDUMP_TLV_MEM   = 2
+	COREDUMP_TLV_REGS  = 3
+)
+
+const (
+	COREDUMP_MAGIC = 0x690c47c3
+)
+
+type coreDumpHdr struct {
+	Magic uint32
+	Size  uint32
+}
+
+type coreDumpTlv struct {
+	Type uint8
+	pad  uint8
+	Len  uint16
+	Off  uint32
+}
+
+func NewCoreConvert() *CoreConvert {
+	return &CoreConvert{}
+}
+
+func (cc *CoreConvert) readHdr() error {
+	var hdr coreDumpHdr
+
+	hdr_buf := make([]byte, binary.Size(hdr))
+	if hdr_buf == nil {
+		return util.NewNewtError("Out of memory")
+	}
+
+	cnt, err := cc.Source.Read(hdr_buf)
+	if err != nil {
+		return util.NewNewtError(fmt.Sprintf("Error reading: %s", err.Error()))
+	}
+	if cnt != binary.Size(hdr) {
+		return util.NewNewtError("Short read")
+	}
+
+	hdr.Magic = binary.LittleEndian.Uint32(hdr_buf[0:4])
+	hdr.Size = binary.LittleEndian.Uint32(hdr_buf[4:8])
+
+	if hdr.Magic != COREDUMP_MAGIC {
+		return util.NewNewtError("Source file is not corefile")
+	}
+	return nil
+}
+
+func (cc *CoreConvert) readTlv() (*coreDumpTlv, error) {
+	var tlv coreDumpTlv
+
+	tlv_buf := make([]byte, binary.Size(tlv))
+	if tlv_buf == nil {
+		return nil, util.NewNewtError("Out of memory")
+	}
+
+	cnt, err := cc.Source.Read(tlv_buf)
+	if err == io.EOF {
+		return nil, nil
+	}
+	if err != nil {
+		return nil, util.NewNewtError(fmt.Sprintf("Error reading: %s",
+			err.Error()))
+	}
+	if cnt == 0 {
+		return nil, nil
+	}
+	if cnt != binary.Size(tlv) {
+		return nil, util.NewNewtError("Short read")
+	}
+
+	tlv.Type = uint8(tlv_buf[0])
+	tlv.pad = uint8(tlv_buf[1])
+	tlv.Len = binary.LittleEndian.Uint16(tlv_buf[2:4])
+	tlv.Off = binary.LittleEndian.Uint32(tlv_buf[4:8])
+
+	return &tlv, nil
+}
+
+func (cc *CoreConvert) makeElfHdr() {
+	var hdr elf.Header32
+	var phdr elf.Prog32
+	var shdr elf.Section32
+
+	copy(hdr.Ident[:], elf.ELFMAG)
+	hdr.Ident[elf.EI_CLASS] = byte(elf.ELFCLASS32)
+	hdr.Ident[elf.EI_DATA] = byte(elf.ELFDATA2LSB)
+	hdr.Ident[elf.EI_VERSION] = byte(elf.EV_CURRENT)
+	hdr.Ident[elf.EI_OSABI] = byte(elf.ELFOSABI_NONE)
+	hdr.Ident[elf.EI_ABIVERSION] = 0
+	hdr.Ident[elf.EI_PAD] = 0
+	hdr.Type = uint16(elf.ET_CORE)
+	hdr.Machine = uint16(elf.EM_ARM)
+	hdr.Version = uint32(elf.EV_CURRENT)
+	hdr.Entry = 0
+	hdr.Phoff = uint32(binary.Size(hdr))
+	hdr.Shoff = 0
+	hdr.Flags = 0
+	hdr.Ehsize = uint16(binary.Size(hdr))
+	hdr.Phentsize = uint16(binary.Size(phdr))
+	hdr.Phnum = uint16(len(cc.phdrs))
+	hdr.Shentsize = uint16(binary.Size(shdr))
+	hdr.Shnum = 0
+	hdr.Shstrndx = uint16(elf.SHN_UNDEF)
+
+	cc.elfHdr = &hdr
+}
+
+func (cc *CoreConvert) makeProgHdr(off uint32, mem []byte) {
+	var phdr elf.Prog32
+
+	memSz := uint32(len(mem))
+
+	phdr.Type = uint32(elf.PT_LOAD)
+	phdr.Off = 0 /* offset of data in file */
+	phdr.Vaddr = off
+	phdr.Paddr = 0
+	phdr.Filesz = memSz
+	phdr.Memsz = memSz
+	phdr.Flags = uint32(elf.PF_R)
+	phdr.Align = 4
+
+	cc.phdrs = append(cc.phdrs, &phdr)
+	if memSz%4 != 0 {
+		pad := make([]byte, 4-memSz%4)
+		mem = append(mem, pad...)
+	}
+	cc.data = append(cc.data, mem)
+}
+
+func (cc *CoreConvert) makeRegData(regs []byte) []byte {
+	type Elf32_Note struct {
+		Namesz uint32
+		Descsz uint32
+		Ntype  uint32
+	}
+
+	type Elf32_Prstatus struct {
+		Dummy  [18]uint32
+		Regs   [18]uint32
+		Dummy2 uint32
+	}
+
+	var note Elf32_Note
+	var sts Elf32_Prstatus
+
+	idx := 0
+	for off := 0; off < len(regs); off += 4 {
+		reg := binary.LittleEndian.Uint32(regs[off : off+4])
+		sts.Regs[idx] = reg
+		idx++
+		if idx >= 18 {
+			break
+		}
+	}
+
+	noteName := ".reg"
+	noteLen := len(noteName) + 1
+	if noteLen%4 != 0 {
+		noteLen = noteLen + 4 - (noteLen % 4)
+	}
+	noteBytes := make([]byte, noteLen)
+	copy(noteBytes[:], noteName)
+
+	note.Namesz = uint32(len(noteName) + 1) /* include terminating '\0' */
+	note.Descsz = uint32(binary.Size(sts))
+	note.Ntype = uint32(elf.NT_PRSTATUS)
+
+	buffer := new(bytes.Buffer)
+	binary.Write(buffer, binary.LittleEndian, note)
+	buffer.Write(noteBytes)
+	binary.Write(buffer, binary.LittleEndian, sts)
+	return buffer.Bytes()
+}
+
+func (cc *CoreConvert) makeRegInfo(regs []byte) {
+	var phdr elf.Prog32
+
+	phdr.Type = uint32(elf.PT_NOTE)
+	phdr.Off = 0
+	phdr.Vaddr = 0
+	phdr.Paddr = 0
+	phdr.Filesz = 0
+	phdr.Memsz = 0
+	phdr.Flags = 0
+	phdr.Align = 4
+
+	data := cc.makeRegData(regs)
+	phdr.Filesz = uint32(len(data))
+
+	cc.phdrs = append(cc.phdrs, &phdr)
+	cc.data = append(cc.data, data)
+}
+
+func (cc *CoreConvert) setProgHdrOff() {
+	off := binary.Size(cc.elfHdr)
+	off += len(cc.phdrs) * binary.Size(cc.phdrs[0])
+
+	for idx, phdr := range cc.phdrs {
+		phdr.Off = uint32(off)
+		off += len(cc.data[idx])
+	}
+}
+
+func (cc *CoreConvert) Convert() error {
+	if cc.Source == nil || cc.Target == nil {
+		return util.NewNewtError("Missing file parameters")
+	}
+
+	err := cc.readHdr()
+	if err != nil {
+		return err
+	}
+
+	for {
+		tlv, err := cc.readTlv()
+		if err != nil {
+			return err
+		}
+		if tlv == nil {
+			break
+		}
+		data_buf := make([]byte, tlv.Len)
+		cnt, err := cc.Source.Read(data_buf)
+		if err != nil {
+			return util.NewNewtError(fmt.Sprintf("Error reading: %s",
+				err.Error()))
+		}
+		if cnt != int(tlv.Len) {
+			return util.NewNewtError("Short file")
+		}
+		switch tlv.Type {
+		case COREDUMP_TLV_MEM:
+			cc.makeProgHdr(tlv.Off, data_buf)
+		case COREDUMP_TLV_IMAGE:
+			cc.ImageHash = data_buf
+		case COREDUMP_TLV_REGS:
+			if tlv.Len%4 != 0 {
+				return util.NewNewtError("Invalid register area size")
+			}
+			cc.makeRegInfo(data_buf)
+		default:
+			return util.NewNewtError("Unknown TLV type")
+		}
+	}
+	cc.makeElfHdr()
+	if err != nil {
+		return err
+	}
+	cc.setProgHdrOff()
+
+	binary.Write(cc.Target, binary.LittleEndian, cc.elfHdr)
+	for _, phdr := range cc.phdrs {
+		binary.Write(cc.Target, binary.LittleEndian, phdr)
+	}
+	for _, data := range cc.data {
+		cc.Target.Write(data)
+	}
+	return nil
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/de7ece74/newtmgr/protocol/coreerase.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/coreerase.go b/newtmgr/protocol/coreerase.go
new file mode 100644
index 0000000..50e853d
--- /dev/null
+++ b/newtmgr/protocol/coreerase.go
@@ -0,0 +1,63 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package protocol
+
+import (
+	"encoding/json"
+	"fmt"
+
+	"mynewt.apache.org/newt/util"
+)
+
+type CoreErase struct {
+	ErrCode uint32 `json:"rc"`
+}
+
+func NewCoreErase() (*CoreErase, error) {
+	ce := &CoreErase{}
+
+	return ce, nil
+}
+
+func (ce *CoreErase) EncodeWriteRequest() (*NmgrReq, error) {
+	nmr, err := NewNmgrReq()
+	if err != nil {
+		return nil, err
+	}
+
+	nmr.Op = NMGR_OP_WRITE
+	nmr.Flags = 0
+	nmr.Group = NMGR_GROUP_ID_IMAGE
+	nmr.Id = IMGMGR_NMGR_OP_CORELOAD
+	nmr.Len = 0
+
+	return nmr, nil
+}
+
+func DecodeCoreEraseResponse(data []byte) (*CoreErase, error) {
+	ce := &CoreErase{}
+
+	err := json.Unmarshal(data, &ce)
+	if err != nil {
+		return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s",
+			err.Error()))
+	}
+	return ce, nil
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/de7ece74/newtmgr/protocol/corelist.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/corelist.go b/newtmgr/protocol/corelist.go
new file mode 100644
index 0000000..a482c97
--- /dev/null
+++ b/newtmgr/protocol/corelist.go
@@ -0,0 +1,63 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package protocol
+
+import (
+	"encoding/json"
+	"fmt"
+
+	"mynewt.apache.org/newt/util"
+)
+
+type CoreList struct {
+	ErrCode uint32 `json:"rc"`
+}
+
+func NewCoreList() (*CoreList, error) {
+	ce := &CoreList{}
+
+	return ce, nil
+}
+
+func (ce *CoreList) EncodeWriteRequest() (*NmgrReq, error) {
+	nmr, err := NewNmgrReq()
+	if err != nil {
+		return nil, err
+	}
+
+	nmr.Op = NMGR_OP_READ
+	nmr.Flags = 0
+	nmr.Group = NMGR_GROUP_ID_IMAGE
+	nmr.Id = IMGMGR_NMGR_OP_CORELIST
+	nmr.Len = 0
+
+	return nmr, nil
+}
+
+func DecodeCoreListResponse(data []byte) (*CoreList, error) {
+	cl := &CoreList{}
+
+	err := json.Unmarshal(data, &cl)
+	if err != nil {
+		return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s",
+			err.Error()))
+	}
+	return cl, nil
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/de7ece74/newtmgr/protocol/coreload.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/coreload.go b/newtmgr/protocol/coreload.go
new file mode 100644
index 0000000..ea2bf4a
--- /dev/null
+++ b/newtmgr/protocol/coreload.go
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package protocol
+
+import (
+	"encoding/base64"
+	"encoding/json"
+	"fmt"
+	"io"
+	"os"
+
+	"mynewt.apache.org/newt/util"
+)
+
+type CoreDownload struct {
+	File   *os.File
+	Runner *CmdRunner
+	Size   int
+}
+
+type coreLoadReq struct {
+	Off uint32 `json:"off"`
+}
+
+type coreLoadResp struct {
+	ErrCode uint32 `json:"rc"`
+	Off     uint32 `json:"off"`
+	Data    string
+}
+
+func NewCoreDownload() (*CoreDownload, error) {
+	f := &CoreDownload{}
+
+	return f, nil
+}
+
+func (cl *CoreDownload) Download() error {
+	if cl.File == nil {
+		return util.NewNewtError("Missing target file")
+	}
+	if cl.Runner == nil {
+		return util.NewNewtError("Missing target")
+	}
+
+	imageDone := 0
+
+	nmr, err := NewNmgrReq()
+	if err != nil {
+		return err
+	}
+	req := &coreLoadReq{}
+	var off uint32 = 0
+
+	for imageDone != 1 {
+		req.Off = off
+		data, _ := json.Marshal(req)
+
+		nmr.Op = NMGR_OP_READ
+		nmr.Flags = 0
+		nmr.Group = NMGR_GROUP_ID_IMAGE
+		nmr.Id = IMGMGR_NMGR_OP_CORELOAD
+		nmr.Len = uint16(len(data))
+		nmr.Data = data
+
+		if err := cl.Runner.WriteReq(nmr); err != nil {
+			return err
+		}
+
+		nmRsp, err := cl.Runner.ReadResp()
+		if err != nil {
+			return err
+		}
+
+		clRsp := coreLoadResp{}
+		if err = json.Unmarshal(nmRsp.Data, &clRsp); err != nil {
+			return util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s",
+				err.Error()))
+		}
+		if clRsp.ErrCode == NMGR_ERR_ENOENT {
+			return util.NewNewtError("No corefile present")
+		}
+		if clRsp.ErrCode != 0 {
+			return util.NewNewtError(fmt.Sprintf("Download failed: %d",
+				clRsp.ErrCode))
+		}
+
+		if off != clRsp.Off {
+			return util.NewNewtError(
+				fmt.Sprintf("Invalid data offset %d, expected %d",
+					clRsp.Off, off))
+		}
+
+		data, err = base64.StdEncoding.DecodeString(clRsp.Data)
+		if err != nil {
+			return util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s",
+				err.Error()))
+		}
+		if len(data) > 0 {
+			n, err := cl.File.Write(data)
+			if err == nil && n < len(data) {
+				err = io.ErrShortWrite
+			}
+			if err != nil {
+				return util.NewNewtError(
+					fmt.Sprintf("Cannot write to file: %s",
+						err.Error()))
+			}
+			off += uint32(len(data))
+		} else {
+			imageDone = 1
+		}
+	}
+	return nil
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/de7ece74/newtmgr/protocol/imagelist.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/imagelist.go b/newtmgr/protocol/imagelist.go
index e6e8172..ea516e3 100644
--- a/newtmgr/protocol/imagelist.go
+++ b/newtmgr/protocol/imagelist.go
@@ -31,10 +31,12 @@ type ImageList struct {
 }
 
 const (
-	IMGMGR_NMGR_OP_LIST   = 0
-	IMGMGR_NMGR_OP_UPLOAD = 1
-	IMGMGR_NMGR_OP_BOOT   = 2
-	IMGMGR_NMGR_OP_FILE   = 3
+	IMGMGR_NMGR_OP_LIST     = 0
+	IMGMGR_NMGR_OP_UPLOAD   = 1
+	IMGMGR_NMGR_OP_BOOT     = 2
+	IMGMGR_NMGR_OP_FILE     = 3
+	IMGMGR_NMGR_OP_CORELIST = 6
+	IMGMGR_NMGR_OP_CORELOAD = 7
 )
 
 func NewImageList() (*ImageList, error) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/de7ece74/newtmgr/protocol/nmgr.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/nmgr.go b/newtmgr/protocol/nmgr.go
index 331c8b2..310c8c1 100644
--- a/newtmgr/protocol/nmgr.go
+++ b/newtmgr/protocol/nmgr.go
@@ -45,6 +45,15 @@ const (
 	NMGR_OP_WRITE_RSP = 3
 )
 
+const (
+	NMGR_ERR_OK       = 0
+	NMGR_ERR_EUNKNOWN = 1
+	NMGR_ERR_ENOMEM   = 2
+	NMGR_ERR_EINVAL   = 3
+	NMGR_ERR_ETIMEOUT = 4
+	NMGR_ERR_ENOENT   = 5
+)
+
 func NewNmgrReq() (*NmgrReq, error) {
 	nmr := &NmgrReq{}
 	nmr.Data = []byte{}


[2/2] incubator-mynewt-newt git commit: newtmgr; go formatting.

Posted by ma...@apache.org.
newtmgr; go formatting.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/ff51f073
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/ff51f073
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/ff51f073

Branch: refs/heads/develop
Commit: ff51f0731d52e04464c6333ea471616155deb3f6
Parents: de7ece7
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu May 19 17:36:07 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Thu May 19 17:36:07 2016 -0700

----------------------------------------------------------------------
 newtmgr/cli/config.go      | 1 +
 newtmgr/cli/connprofile.go | 1 +
 newtmgr/cli/echo.go        | 1 +
 newtmgr/cli/mpstats.go     | 1 +
 newtmgr/cli/stats.go       | 1 +
 newtmgr/cli/taskstats.go   | 1 +
 newtmgr/cli/usage.go       | 1 +
 7 files changed, 7 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/ff51f073/newtmgr/cli/config.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/config.go b/newtmgr/cli/config.go
index 405eba2..76a8c07 100644
--- a/newtmgr/cli/config.go
+++ b/newtmgr/cli/config.go
@@ -26,6 +26,7 @@ import (
 	"mynewt.apache.org/newt/newtmgr/protocol"
 	"mynewt.apache.org/newt/newtmgr/transport"
 	"mynewt.apache.org/newt/util"
+
 	"github.com/spf13/cobra"
 )
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/ff51f073/newtmgr/cli/connprofile.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/connprofile.go b/newtmgr/cli/connprofile.go
index 5035dc1..19511e0 100644
--- a/newtmgr/cli/connprofile.go
+++ b/newtmgr/cli/connprofile.go
@@ -25,6 +25,7 @@ import (
 
 	"mynewt.apache.org/newt/newtmgr/config"
 	"mynewt.apache.org/newt/util"
+
 	"github.com/spf13/cobra"
 )
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/ff51f073/newtmgr/cli/echo.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/echo.go b/newtmgr/cli/echo.go
index 3f0e104..62b08bb 100644
--- a/newtmgr/cli/echo.go
+++ b/newtmgr/cli/echo.go
@@ -25,6 +25,7 @@ import (
 	"mynewt.apache.org/newt/newtmgr/config"
 	"mynewt.apache.org/newt/newtmgr/protocol"
 	"mynewt.apache.org/newt/newtmgr/transport"
+
 	"github.com/spf13/cobra"
 )
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/ff51f073/newtmgr/cli/mpstats.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/mpstats.go b/newtmgr/cli/mpstats.go
index 8a4844a..24cae5e 100644
--- a/newtmgr/cli/mpstats.go
+++ b/newtmgr/cli/mpstats.go
@@ -25,6 +25,7 @@ import (
 	"mynewt.apache.org/newt/newtmgr/config"
 	"mynewt.apache.org/newt/newtmgr/protocol"
 	"mynewt.apache.org/newt/newtmgr/transport"
+
 	"github.com/spf13/cobra"
 )
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/ff51f073/newtmgr/cli/stats.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/stats.go b/newtmgr/cli/stats.go
index cbfcd5a..d7d963b 100644
--- a/newtmgr/cli/stats.go
+++ b/newtmgr/cli/stats.go
@@ -25,6 +25,7 @@ import (
 	"mynewt.apache.org/newt/newtmgr/config"
 	"mynewt.apache.org/newt/newtmgr/protocol"
 	"mynewt.apache.org/newt/newtmgr/transport"
+
 	"github.com/spf13/cobra"
 )
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/ff51f073/newtmgr/cli/taskstats.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/taskstats.go b/newtmgr/cli/taskstats.go
index c9ec687..228fa59 100644
--- a/newtmgr/cli/taskstats.go
+++ b/newtmgr/cli/taskstats.go
@@ -25,6 +25,7 @@ import (
 	"mynewt.apache.org/newt/newtmgr/config"
 	"mynewt.apache.org/newt/newtmgr/protocol"
 	"mynewt.apache.org/newt/newtmgr/transport"
+
 	"github.com/spf13/cobra"
 )
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/ff51f073/newtmgr/cli/usage.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/usage.go b/newtmgr/cli/usage.go
index 123b61f..9b72c8a 100644
--- a/newtmgr/cli/usage.go
+++ b/newtmgr/cli/usage.go
@@ -24,6 +24,7 @@ import (
 	"os"
 
 	"mynewt.apache.org/newt/util"
+
 	"github.com/spf13/cobra"
 )