Skip to content

Commit

Permalink
Small enhancements
Browse files Browse the repository at this point in the history
parts: Allow removal of parts from list
firmware: Fix removal if just one firmware entry is present
  • Loading branch information
sgissi committed Jun 20, 2020
1 parent 5235161 commit b4bd0b1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 34 deletions.
44 changes: 38 additions & 6 deletions cmd/fwtablectl/firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@ import (
"github.com/sgissi/wdmch-tools/pkg/fwtable"
)

func firmware(args []string) {
switch args[0] {
case "new":
if len(args) != 6 {
fmt.Println("Wrong number of parameters for 'firmware new'")
usage()
}
firmwareNew(args[1:])
case "types":
fmt.Println("Firmware Types:")
for _, t := range fwtable.FwTypes {
fmt.Println(" ", t)
}
case "update":
if len(args) != 4 {
fmt.Println("Wrong number of parameters for 'firmware update'")
usage()
}
firmwareUpdate(args[1:])
case "remove":
if len(args) != 3 {
fmt.Println("Wrong number of parameters for 'firmware remove'")
usage()
}
firmwareRemove(args[1:])
}
}

func firmwareNew(args []string) {
fwtFile := args[0]
fwType := args[1]
Expand Down Expand Up @@ -99,12 +127,16 @@ func firmwareRemove(args []string) {
fmt.Printf("Could not find firmware entry with type '%s'\n", fwType)
os.Exit(1)
}
// Move items after index back one position
copy(fw.Fws[idx:], fw.Fws[idx+1:])
// Release last item
fw.Fws[len(fw.Fws)-1] = nil
// Truncate
fw.Fws = fw.Fws[:len(fw.Fws)-1]
if len(fw.Fws) == 1 {
fw.Fws = nil
} else {
// Move items after index back one position
copy(fw.Fws[idx:], fw.Fws[idx+1:])
// Release last item
fw.Fws[len(fw.Fws)-1] = nil
// Truncate
fw.Fws = fw.Fws[:len(fw.Fws)-1]
}
// Save table
writeFwTable(fwtFile, fw)
}
Expand Down
35 changes: 7 additions & 28 deletions cmd/fwtablectl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,17 @@ func usage() {
fmt.Println(" firmware update [fwtable-file] [type] [file] ....................... - Update entry with new file")
fmt.Println(" firmware remove [fwtable-file] [type] .............................. - Remove firmware from table")
fmt.Println(" firmware types .................................................... - Print available firmware types")
fmt.Println(" part remove [fwtable-file] [index] - Remove part by index")
fmt.Println("---")
fmt.Println("Examples:")
fmt.Println(" fwtablectl show /dev/sataa1")
fmt.Println(" fwtablectl firmware update fwtable.bin KernelRootFS rootfs.cpio.gz")
fmt.Println(" fwtablectl firmware new /dev/sataa1 KernelRootFS rootfs.cpio.gz 67584 0x02200000")
fmt.Println(" fwtablectl firmware remove /root/fwtable.bin uBoot")
fmt.Println(" fwtablectl part remove /dev/sda1 1")
os.Exit(1)
}

func firmware(args []string) {
switch args[0] {
case "new":
if len(args) != 6 {
fmt.Println("Wrong number of parameters for 'firmware new'")
usage()
}
firmwareNew(args[1:])
case "types":
fmt.Println("Firmware Types:")
for _, t := range fwtable.FwTypes {
fmt.Println(" ", t)
}
case "update":
if len(args) != 4 {
fmt.Println("Wrong number of parameters for 'firmware update'")
usage()
}
firmwareUpdate(args[1:])
case "remove":
if len(args) != 3 {
fmt.Println("Wrong number of parameters for 'firmware remove'")
usage()
}
firmwareRemove(args[1:])
}
}

func main() {

if len(os.Args) < 2 {
Expand All @@ -69,6 +43,11 @@ func main() {
usage()
}
firmware(os.Args[2:])
case "part":
if len(os.Args) < 3 {
usage()
}
part(os.Args[2:])
default:
fmt.Println("Invalid command:", os.Args[1])
usage()
Expand Down
45 changes: 45 additions & 0 deletions cmd/fwtablectl/part.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
"os"
"strconv"
)

func part(args []string) {
switch args[0] {
case "remove":
if len(args) != 3 {
fmt.Println("Wrong number of arguments for 'parts remove'")
usage()
}
partRemove(args[1:])
}
}

func partRemove(args []string) {
fwtFile := args[0]
index, err := strconv.ParseInt(args[1], 10, 8)
if err != nil {
fmt.Println("Invalid number for index:", args[1], err)
os.Exit(1)
}
idx := int(index)
fw := readFwTable(fwtFile)
if idx < 1 || idx > len(fw.Parts) {
fmt.Println("Invalid part index:", idx)
os.Exit(1)
}
if len(fw.Parts) == 1 {
fw.Parts = nil
} else {
// Move items after index back one position
copy(fw.Parts[idx:], fw.Parts[idx+1:])
// Release last item
fw.Parts[len(fw.Parts)-1] = nil
// Truncate
fw.Parts = fw.Parts[:len(fw.Parts)-1]
}
// Save table
writeFwTable(fwtFile, fw)
}

0 comments on commit b4bd0b1

Please sign in to comment.