|
| 1 | +// This file is part of arduino-cloud-cli. |
| 2 | +// |
| 3 | +// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as published |
| 7 | +// by the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +package ota |
| 19 | + |
| 20 | +import ( |
| 21 | + "encoding/binary" |
| 22 | + "fmt" |
| 23 | + "hash/crc32" |
| 24 | + "io" |
| 25 | + "os" |
| 26 | + "strconv" |
| 27 | + "strings" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + ErrCRC32Mismatch = fmt.Errorf("CRC32 mismatch") |
| 32 | +) |
| 33 | + |
| 34 | +type OtaFirmwareHeader struct { |
| 35 | + Length uint32 |
| 36 | + CRC32 uint32 |
| 37 | + MagicNumber uint32 |
| 38 | + BoardType string |
| 39 | + FQBN *string |
| 40 | + VID string |
| 41 | + PID string |
| 42 | + IsArduinoBoard bool |
| 43 | +} |
| 44 | + |
| 45 | +func readBytes(file *os.File, length int, offset int64) ([]byte, error) { |
| 46 | + bytes := make([]byte, length) |
| 47 | + _, err := file.ReadAt(bytes, offset) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + return bytes, nil |
| 52 | +} |
| 53 | + |
| 54 | +func crc32Buffered(file *os.File) (uint32, error) { |
| 55 | + h := crc32.NewIEEE() |
| 56 | + // Discard first 8 bytes |
| 57 | + file.Seek(8, 0) |
| 58 | + // Read file in chunks and compute CRC32 |
| 59 | + buf := make([]byte, 4096) |
| 60 | + for { |
| 61 | + n, err := file.Read(buf) |
| 62 | + if err != nil && err != io.EOF { |
| 63 | + return 0, err |
| 64 | + } |
| 65 | + if n == 0 { |
| 66 | + break |
| 67 | + } |
| 68 | + h.Write(buf[:n]) |
| 69 | + } |
| 70 | + return h.Sum32(), nil |
| 71 | +} |
| 72 | + |
| 73 | +func extractXID(buff []byte) string { |
| 74 | + xid := strconv.FormatUint(uint64(binary.LittleEndian.Uint16(buff)), 16) |
| 75 | + return strings.ToUpper(xid) |
| 76 | +} |
| 77 | + |
| 78 | +// DecodeOtaFirmwareHeader decodes the OTA firmware header from a binary file. |
| 79 | +// File is composed by an header and a payload (optionally lzss compressed). |
| 80 | +// Method is also checking CRC32 of the file, verifying that file is not corrupted. |
| 81 | +// See: https://arduino.atlassian.net/wiki/spaces/RFC/pages/1616871540/OTA+header+structure |
| 82 | +func DecodeOtaFirmwareHeader(binaryFilePath string) (*OtaFirmwareHeader, error) { |
| 83 | + // Check if file exists |
| 84 | + if _, err := os.Stat(binaryFilePath); err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + // Open file |
| 88 | + if otafileptr, err := os.Open(binaryFilePath); err != nil { |
| 89 | + return nil, err |
| 90 | + } else { |
| 91 | + defer otafileptr.Close() |
| 92 | + |
| 93 | + // Get length (payload + header without lenght and CRC32 bytes) |
| 94 | + buff, err := readBytes(otafileptr, 4, 0) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + lenghtInt := binary.LittleEndian.Uint32(buff) |
| 99 | + |
| 100 | + // Get CRC32 (uint32) |
| 101 | + buff, err = readBytes(otafileptr, 4, 4) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + readsum := binary.LittleEndian.Uint32(buff) |
| 106 | + |
| 107 | + // Read full binary file (buffered), starting from 8th byte (magic number) |
| 108 | + computedsum, err := crc32Buffered(otafileptr) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + if computedsum != readsum { |
| 113 | + return nil, ErrCRC32Mismatch |
| 114 | + } |
| 115 | + |
| 116 | + // Get PID+VID (uint32) |
| 117 | + buff, err = readBytes(otafileptr, 8, 8) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + completeMagicNumber := binary.LittleEndian.Uint32(buff) |
| 122 | + |
| 123 | + //Extract VID and PID |
| 124 | + pid := extractXID(buff[:2]) |
| 125 | + vid := extractXID(buff[2:]) |
| 126 | + |
| 127 | + boardType, fqbn, isArduino := getBoardType(completeMagicNumber, pid) |
| 128 | + |
| 129 | + return &OtaFirmwareHeader{ |
| 130 | + Length: lenghtInt, |
| 131 | + CRC32: computedsum, |
| 132 | + BoardType: boardType, |
| 133 | + MagicNumber: completeMagicNumber, |
| 134 | + IsArduinoBoard: isArduino, |
| 135 | + PID: pid, |
| 136 | + VID: vid, |
| 137 | + FQBN: fqbn, |
| 138 | + }, nil |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func getBoardType(magicNumber uint32, pid string) (string, *string, bool) { |
| 143 | + baordType := "UNKNOWN" |
| 144 | + if t, ok := BoardTypes[magicNumber]; ok { |
| 145 | + baordType = t |
| 146 | + } |
| 147 | + isArduino := baordType != "UNKNOWN" && baordType != "ESP32" |
| 148 | + var fqbn *string |
| 149 | + if t, ok := ArduinoPidToFQBN[pid]; ok { |
| 150 | + fqbn = &t |
| 151 | + } |
| 152 | + |
| 153 | + return baordType, fqbn, isArduino |
| 154 | +} |
0 commit comments