Tim created MYNEWT-364:
--------------------------

             Summary: Don't use bash scripts
                 Key: MYNEWT-364
                 URL: https://issues.apache.org/jira/browse/MYNEWT-364
             Project: Mynewt
          Issue Type: Bug
          Components: Newt
    Affects Versions: v0_9_0
         Environment: Ubuntu 14.10 using the Docker version of newt
            Reporter: Tim
            Assignee: Sterling Hughes


The download and debug scripts are currently written using Bash. That is 
unfortunate because Bash is very error-prone and scripts tend to be buggy. 
Also, it makes distribution on Windows much harder.

You're already using Go, I'd suggest rewriting them using that. Alternatives 
are Python, Powershell, Lua, etc.

I've rewritten nrf52_download.sh in Go (below; compiled but not tested). It was 
very easy, and is only 7 lines longer (due to extra error checking/reporting).

The main downside is the size - 2.7 MB, but it can be reduced to 523 kB by 
stripping debug symbols and compressing it with upx. I think that is reasonable 
for now. You can combine debug and download scripts for further savings if 
necessary.

{code}
// 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 main

import (
        "fmt"
        "io/ioutil"
        "log"
        "os"
        "os/exec"
        "strings"
)

func main() {

        if len(os.Args) == 1 {
                log.Fatalf(`
Usage: {} <bsp_directory_path> <binary> [features...]
  - bsp_directory_path is absolute path to hw/bsp/bsp_name
  - binary is the path to prefix to target binary, .elf.bin appended to this
    name is the raw binary format of the binary.
  - features are the target features. So you can have e.g. different
    flash offset for bootloader 'feature')
`, os.Args[0])
        }

        if len(os.Args) < 3 {
                log.Fatal("Need binary to download")
        }

        // Look for 'bootloader' from 3rd arg onwards (in the features)
        isBootloader := false
        if len(os.Args) > 3 {
                for i := range os.Args[3:] {
                        if os.Args[i] == "bootloader" {
                                isBootloader = true
                        }
                }
        }

        basename := os.Args[2]
        flashOffset := 0x8000
        filename := basename + ".img"

        if isBootloader {
                flashOffset = 0
                filename = basename + ".elf.bin"
        }

        gdbCmdFile := ".gdb_cmds"

        log.Printf("Downloading %s to %#x", filename, flashOffset)

        // XXX for some reason JLinkExe overwrites flash at offset 0 when
        // downloading somewhere in the flash. So need to figure out how to 
tell it
        // not to do that, or report failure if gdb fails to write this file
        gdbCmds := fmt.Sprintf(`
shell /bin/sh -c 'trap \"\" 2;JLinkGDBServer -device nRF52 -speed 4000 -if SWD 
-port 3333 -singlerun' & 
target remote localhost:3333
restore %s binary %#x
quit
`, filename, flashOffset)

        err := ioutil.WriteFile(gdbCmdFile, []byte(gdbCmds), 0664)
        if err != nil {
                log.Fatalf("Error writing to %s: %v", gdbCmdFile, err)
        }
        defer os.Remove(gdbCmdFile)

        msgs, err := exec.Command("arm-none-eabi-gdb", "-x", 
gdbCmdFile).CombinedOutput()

        ioutil.WriteFile(".gdb_out", msgs, 0664)

        if err != nil {
                log.Fatalf("Error running gdb: %v", err)
        }

        smsgs := string(msgs)

        if strings.Contains(smsgs, "error") ||
                strings.Contains(smsgs, "failed") ||
                strings.Contains(smsgs, "unknown / supported") ||
                strings.Contains(smsgs, "No such file or directory") {
                os.Exit(1)
        }
}
{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to