Skip to content

Instantly share code, notes, and snippets.

@PyYoshi
Last active November 26, 2020 09:29
Show Gist options
  • Save PyYoshi/678ca89b7dabb5f398ac15abc1381c12 to your computer and use it in GitHub Desktop.
Save PyYoshi/678ca89b7dabb5f398ac15abc1381c12 to your computer and use it in GitHub Desktop.
WXR-2533DHP/WXR-2533DHP2用ファームウェアを展開するためのツール(ストリーミング処理を行っていないのでファイルサイズ分メモリを消費します)
$ go build -o firmware-extractor

$ ./firmware-extractor -h
Usage of firmware-extractor:
  -f string
        WXR-2533DHPのファームウェアファイルを指定してください

$ ./firmware-extractor -f ./wxr_2533dhp2_jp_146
Input:
        Firmware Path: /xxxxxxxxx/wxr_2533dhp2_jp_146

Firmware Info:
        Device Name: WXR2533DHP2
        Device Version: 1.46_1.08
        Device Region: JP
        RootFS Image Size: 15292729
        Kernel Image Size: 1828864
        Header Info Size: 128

Output:
        Kernel Image: /xxxxxxxxx/wxr_2533dhp2_jp_146.kernel
        RootFS Image: /xxxxxxxxx/wxr_2533dhp2_jp_146.rootfs

Done!
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
)
const firmwareHeaderInfoSize = 128
var (
flagFirmwareFilePath string
)
func init() {
flag.StringVar(&flagFirmwareFilePath, "f", "", "WXR-2533DHPのファームウェアファイルを指定してください")
}
func main() {
flag.Parse()
if flagFirmwareFilePath == "" {
flag.Usage()
os.Exit(1)
}
firmwareAbspath, err := filepath.Abs(flagFirmwareFilePath)
if err != nil {
panic(err)
}
fmt.Println()
fmt.Println("Input:")
fmt.Println("\tFirmware Path:", firmwareAbspath)
b, err := ioutil.ReadFile(firmwareAbspath)
if err != nil {
panic(err)
}
bf := bytes.NewReader(b)
headerInfoBytes := make([]byte, firmwareHeaderInfoSize)
_, err = bf.Read(headerInfoBytes)
if err != nil {
panic(err)
}
var deviceName string
var deviceVersion string
var deviceRegion string
var rootfsImgSizeStr string
var kernelImgSizeStr string
var headerInfoSizeStr string
fmt.Sscanf(
string(headerInfoBytes),
`device:%s
version:%s
region:%s
RootfsSize:%s
KernelSize:%s
InfoHeadSize:%s
`, // 0A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 32
&deviceName,
&deviceVersion,
&deviceRegion,
&rootfsImgSizeStr,
&kernelImgSizeStr,
&headerInfoSizeStr,
)
fmt.Println()
fmt.Println("Firmware Info:")
fmt.Println("\tDevice Name:", deviceName)
fmt.Println("\tDevice Version:", deviceVersion)
fmt.Println("\tDevice Region:", deviceRegion)
fmt.Println("\tRootFS Image Size:", rootfsImgSizeStr)
fmt.Println("\tKernel Image Size:", kernelImgSizeStr)
fmt.Println("\tHeader Info Size:", headerInfoSizeStr)
rootfsImgSize, err := strconv.ParseInt(rootfsImgSizeStr, 10, 64)
if err != nil {
panic(err)
}
kernelImgSize, err := strconv.ParseInt(kernelImgSizeStr, 10, 64)
if err != nil {
panic(err)
}
outputDir := filepath.Dir(firmwareAbspath)
baseName := filepath.Base(firmwareAbspath)
kernelImgPath := filepath.Join(outputDir, baseName+".kernel")
rootfsImgPath := filepath.Join(outputDir, baseName+".rootfs")
kernelBytes := make([]byte, kernelImgSize)
_, err = bf.Read(kernelBytes)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(kernelImgPath, kernelBytes, 0644)
if err != nil {
panic(err)
}
rootfsBytes := make([]byte, rootfsImgSize)
_, err = bf.Read(rootfsBytes)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(rootfsImgPath, rootfsBytes, 0644)
if err != nil {
panic(err)
}
fmt.Println()
fmt.Println("Output:")
fmt.Println("\tKernel Image:", kernelImgPath)
fmt.Println("\tRootFS Image:", rootfsImgPath)
fmt.Println()
fmt.Println("Done!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment