Markus Paar
/
firmware
firmware update from USB stick, does not work properly. If someone can get it work, please inform me.
FirmwareUpdater.cpp@0:dad421e28e26, 2011-04-01 (annotated)
- Committer:
- Markus_Paar
- Date:
- Fri Apr 01 07:51:52 2011 +0000
- Revision:
- 0:dad421e28e26
0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Markus_Paar | 0:dad421e28e26 | 1 | /** |
Markus_Paar | 0:dad421e28e26 | 2 | * ============================================================================= |
Markus_Paar | 0:dad421e28e26 | 3 | * Firmware updater (Version 0.0.1) |
Markus_Paar | 0:dad421e28e26 | 4 | * ============================================================================= |
Markus_Paar | 0:dad421e28e26 | 5 | * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems) |
Markus_Paar | 0:dad421e28e26 | 6 | * |
Markus_Paar | 0:dad421e28e26 | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Markus_Paar | 0:dad421e28e26 | 8 | * of this software and associated documentation files (the "Software"), to deal |
Markus_Paar | 0:dad421e28e26 | 9 | * in the Software without restriction, including without limitation the rights |
Markus_Paar | 0:dad421e28e26 | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Markus_Paar | 0:dad421e28e26 | 11 | * copies of the Software, and to permit persons to whom the Software is |
Markus_Paar | 0:dad421e28e26 | 12 | * furnished to do so, subject to the following conditions: |
Markus_Paar | 0:dad421e28e26 | 13 | * |
Markus_Paar | 0:dad421e28e26 | 14 | * The above copyright notice and this permission notice shall be included in |
Markus_Paar | 0:dad421e28e26 | 15 | * all copies or substantial portions of the Software. |
Markus_Paar | 0:dad421e28e26 | 16 | * |
Markus_Paar | 0:dad421e28e26 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Markus_Paar | 0:dad421e28e26 | 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Markus_Paar | 0:dad421e28e26 | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Markus_Paar | 0:dad421e28e26 | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Markus_Paar | 0:dad421e28e26 | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Markus_Paar | 0:dad421e28e26 | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Markus_Paar | 0:dad421e28e26 | 23 | * THE SOFTWARE. |
Markus_Paar | 0:dad421e28e26 | 24 | * ============================================================================= |
Markus_Paar | 0:dad421e28e26 | 25 | */ |
Markus_Paar | 0:dad421e28e26 | 26 | |
Markus_Paar | 0:dad421e28e26 | 27 | #include "FirmwareUpdater.h" |
Markus_Paar | 0:dad421e28e26 | 28 | |
Markus_Paar | 0:dad421e28e26 | 29 | #include <stdio.h> |
Markus_Paar | 0:dad421e28e26 | 30 | #include <stdarg.h> |
Markus_Paar | 0:dad421e28e26 | 31 | |
Markus_Paar | 0:dad421e28e26 | 32 | extern "C" void mbed_reset(); |
Markus_Paar | 0:dad421e28e26 | 33 | |
Markus_Paar | 0:dad421e28e26 | 34 | const std::string FirmwareUpdater::EXT_BIN = ".bin"; |
Markus_Paar | 0:dad421e28e26 | 35 | const std::string FirmwareUpdater::EXT_BINTMP = ".b__"; |
Markus_Paar | 0:dad421e28e26 | 36 | const std::string FirmwareUpdater::EXT_TXT = ".txt"; |
Markus_Paar | 0:dad421e28e26 | 37 | const std::string FirmwareUpdater::EXT_TXTTMP = ".t__"; |
Markus_Paar | 0:dad421e28e26 | 38 | |
Markus_Paar | 0:dad421e28e26 | 39 | MSCFileSystem fs ("fs"); |
Markus_Paar | 0:dad421e28e26 | 40 | LocalFileSystem local("local"); |
Markus_Paar | 0:dad421e28e26 | 41 | |
Markus_Paar | 0:dad421e28e26 | 42 | |
Markus_Paar | 0:dad421e28e26 | 43 | /** |
Markus_Paar | 0:dad421e28e26 | 44 | * Create. |
Markus_Paar | 0:dad421e28e26 | 45 | * |
Markus_Paar | 0:dad421e28e26 | 46 | * @param src_name: An application name on USB Stick. Do not include a extention. |
Markus_Paar | 0:dad421e28e26 | 47 | * @param dest_name An application name on MBED. Do not include a extention. |
Markus_Paar | 0:dad421e28e26 | 48 | * @param log True if logging. |
Markus_Paar | 0:dad421e28e26 | 49 | */ |
Markus_Paar | 0:dad421e28e26 | 50 | FirmwareUpdater::FirmwareUpdater(std::string src_name, std::string dest_name, bool log) |
Markus_Paar | 0:dad421e28e26 | 51 | : src_name(src_name), dest_name(dest_name), log(log), local("local") { |
Markus_Paar | 0:dad421e28e26 | 52 | |
Markus_Paar | 0:dad421e28e26 | 53 | } |
Markus_Paar | 0:dad421e28e26 | 54 | |
Markus_Paar | 0:dad421e28e26 | 55 | /* |
Markus_Paar | 0:dad421e28e26 | 56 | * Dispose. |
Markus_Paar | 0:dad421e28e26 | 57 | */ |
Markus_Paar | 0:dad421e28e26 | 58 | FirmwareUpdater::~FirmwareUpdater() { |
Markus_Paar | 0:dad421e28e26 | 59 | } |
Markus_Paar | 0:dad421e28e26 | 60 | |
Markus_Paar | 0:dad421e28e26 | 61 | /** |
Markus_Paar | 0:dad421e28e26 | 62 | * Get the source_name. |
Markus_Paar | 0:dad421e28e26 | 63 | * |
Markus_Paar | 0:dad421e28e26 | 64 | * @return src_name. |
Markus_Paar | 0:dad421e28e26 | 65 | */ |
Markus_Paar | 0:dad421e28e26 | 66 | const std::string FirmwareUpdater:: get_src_name() const { |
Markus_Paar | 0:dad421e28e26 | 67 | return src_name; |
Markus_Paar | 0:dad421e28e26 | 68 | } |
Markus_Paar | 0:dad421e28e26 | 69 | |
Markus_Paar | 0:dad421e28e26 | 70 | /** |
Markus_Paar | 0:dad421e28e26 | 71 | * Get the dest_name. |
Markus_Paar | 0:dad421e28e26 | 72 | * |
Markus_Paar | 0:dad421e28e26 | 73 | * @return _destname. |
Markus_Paar | 0:dad421e28e26 | 74 | */ |
Markus_Paar | 0:dad421e28e26 | 75 | const std::string FirmwareUpdater:: get_dest_name() const { |
Markus_Paar | 0:dad421e28e26 | 76 | return dest_name; |
Markus_Paar | 0:dad421e28e26 | 77 | } |
Markus_Paar | 0:dad421e28e26 | 78 | |
Markus_Paar | 0:dad421e28e26 | 79 | /* |
Markus_Paar | 0:dad421e28e26 | 80 | * Checking a new firmware. |
Markus_Paar | 0:dad421e28e26 | 81 | * Compare versions of the software between local storage on mbed and on USB Stick. |
Markus_Paar | 0:dad421e28e26 | 82 | * |
Markus_Paar | 0:dad421e28e26 | 83 | * @return Return 0 if a new firmware exists. |
Markus_Paar | 0:dad421e28e26 | 84 | */ |
Markus_Paar | 0:dad421e28e26 | 85 | |
Markus_Paar | 0:dad421e28e26 | 86 | int FirmwareUpdater::exist() { |
Markus_Paar | 0:dad421e28e26 | 87 | int ver_local, ver_USB; |
Markus_Paar | 0:dad421e28e26 | 88 | printf("Fetch the version from a local."); |
Markus_Paar | 0:dad421e28e26 | 89 | /* |
Markus_Paar | 0:dad421e28e26 | 90 | * Fetch the version from a local. |
Markus_Paar | 0:dad421e28e26 | 91 | */ |
Markus_Paar | 0:dad421e28e26 | 92 | std::string file_local = "/local/" + dest_name + EXT_TXT; |
Markus_Paar | 0:dad421e28e26 | 93 | FILE *fp = fopen(file_local.c_str(), "rb"); |
Markus_Paar | 0:dad421e28e26 | 94 | if (fp == NULL) { |
Markus_Paar | 0:dad421e28e26 | 95 | LOG("Local firmware version file '%s' open failed.\n\r", file_local.c_str()); |
Markus_Paar | 0:dad421e28e26 | 96 | return -1; |
Markus_Paar | 0:dad421e28e26 | 97 | } |
Markus_Paar | 0:dad421e28e26 | 98 | if (fscanf(fp, "%d", &ver_local) != 1) { |
Markus_Paar | 0:dad421e28e26 | 99 | LOG("Local firmware version file '%s' is invalid.\n\r", file_local.c_str()); |
Markus_Paar | 0:dad421e28e26 | 100 | return -2; |
Markus_Paar | 0:dad421e28e26 | 101 | } |
Markus_Paar | 0:dad421e28e26 | 102 | fclose(fp); |
Markus_Paar | 0:dad421e28e26 | 103 | LOG("Local firmware version is %d. (%s)\n\r", ver_local, file_local.c_str()); |
Markus_Paar | 0:dad421e28e26 | 104 | |
Markus_Paar | 0:dad421e28e26 | 105 | /* |
Markus_Paar | 0:dad421e28e26 | 106 | * Fetch the version from USB Stick. |
Markus_Paar | 0:dad421e28e26 | 107 | */ |
Markus_Paar | 0:dad421e28e26 | 108 | printf("Fetch the version from USB Stick.\r"); |
Markus_Paar | 0:dad421e28e26 | 109 | std::string file_USB = "/fs/" + src_name + EXT_TXT; |
Markus_Paar | 0:dad421e28e26 | 110 | fp = fopen(file_USB.c_str(), "rb"); |
Markus_Paar | 0:dad421e28e26 | 111 | |
Markus_Paar | 0:dad421e28e26 | 112 | if (fp == NULL) { |
Markus_Paar | 0:dad421e28e26 | 113 | LOG("Source firmware version file '%s' open failed.\n\r", file_USB.c_str()); |
Markus_Paar | 0:dad421e28e26 | 114 | return -1; |
Markus_Paar | 0:dad421e28e26 | 115 | } |
Markus_Paar | 0:dad421e28e26 | 116 | if (fscanf(fp, "%d", &ver_USB) != 1) { |
Markus_Paar | 0:dad421e28e26 | 117 | LOG("Source firmware version file '%s' is invalid.\n\r", file_USB.c_str()); |
Markus_Paar | 0:dad421e28e26 | 118 | return -2; |
Markus_Paar | 0:dad421e28e26 | 119 | } |
Markus_Paar | 0:dad421e28e26 | 120 | fclose(fp); |
Markus_Paar | 0:dad421e28e26 | 121 | LOG("Source firmware version is %d. (%s)\n\r", ver_USB, file_USB.c_str()); |
Markus_Paar | 0:dad421e28e26 | 122 | |
Markus_Paar | 0:dad421e28e26 | 123 | |
Markus_Paar | 0:dad421e28e26 | 124 | |
Markus_Paar | 0:dad421e28e26 | 125 | return (ver_local < ver_USB) ? 0 : 1; |
Markus_Paar | 0:dad421e28e26 | 126 | } |
Markus_Paar | 0:dad421e28e26 | 127 | |
Markus_Paar | 0:dad421e28e26 | 128 | /** |
Markus_Paar | 0:dad421e28e26 | 129 | * Execute update. |
Markus_Paar | 0:dad421e28e26 | 130 | * |
Markus_Paar | 0:dad421e28e26 | 131 | * @return Return 0 if it succeed. |
Markus_Paar | 0:dad421e28e26 | 132 | */ |
Markus_Paar | 0:dad421e28e26 | 133 | int FirmwareUpdater::execute() { |
Markus_Paar | 0:dad421e28e26 | 134 | /* |
Markus_Paar | 0:dad421e28e26 | 135 | * Fetch the files. |
Markus_Paar | 0:dad421e28e26 | 136 | */ |
Markus_Paar | 0:dad421e28e26 | 137 | printf("Start execute\r"); |
Markus_Paar | 0:dad421e28e26 | 138 | std::string USB_txt = "/fs/" + src_name + EXT_TXT; |
Markus_Paar | 0:dad421e28e26 | 139 | std::string USB_bin = "/fs/" + src_name + EXT_BIN; |
Markus_Paar | 0:dad421e28e26 | 140 | |
Markus_Paar | 0:dad421e28e26 | 141 | /* |
Markus_Paar | 0:dad421e28e26 | 142 | std::string file_txttmp = "/local/" + name + EXT_TXTTMP; |
Markus_Paar | 0:dad421e28e26 | 143 | std::string file_bintmp = "/local/" + name + EXT_BINTMP; |
Markus_Paar | 0:dad421e28e26 | 144 | if (fetch(serv_txt, file_txttmp) != 0) { |
Markus_Paar | 0:dad421e28e26 | 145 | return -1; |
Markus_Paar | 0:dad421e28e26 | 146 | } |
Markus_Paar | 0:dad421e28e26 | 147 | if (fetch(serv_bin, file_bintmp) != 0) { |
Markus_Paar | 0:dad421e28e26 | 148 | return -2; |
Markus_Paar | 0:dad421e28e26 | 149 | }*/ |
Markus_Paar | 0:dad421e28e26 | 150 | /* |
Markus_Paar | 0:dad421e28e26 | 151 | * Copy it. |
Markus_Paar | 0:dad421e28e26 | 152 | */ |
Markus_Paar | 0:dad421e28e26 | 153 | |
Markus_Paar | 0:dad421e28e26 | 154 | std::string local_txt = "/local/" + dest_name + EXT_TXT; |
Markus_Paar | 0:dad421e28e26 | 155 | std::string local_bin = "/local/" + dest_name + EXT_BIN; |
Markus_Paar | 0:dad421e28e26 | 156 | if (copy(USB_txt, local_txt) != 0) { |
Markus_Paar | 0:dad421e28e26 | 157 | printf("-3"); |
Markus_Paar | 0:dad421e28e26 | 158 | return -3; |
Markus_Paar | 0:dad421e28e26 | 159 | } |
Markus_Paar | 0:dad421e28e26 | 160 | if (copy(USB_bin, local_bin) != 0) { |
Markus_Paar | 0:dad421e28e26 | 161 | printf("-4"); |
Markus_Paar | 0:dad421e28e26 | 162 | return -4; |
Markus_Paar | 0:dad421e28e26 | 163 | } |
Markus_Paar | 0:dad421e28e26 | 164 | /* |
Markus_Paar | 0:dad421e28e26 | 165 | * Delete the temporary files. |
Markus_Paar | 0:dad421e28e26 | 166 | */ |
Markus_Paar | 0:dad421e28e26 | 167 | //remove(file_txttmp.c_str()); |
Markus_Paar | 0:dad421e28e26 | 168 | //remove(file_bintmp.c_str()); |
Markus_Paar | 0:dad421e28e26 | 169 | return 0; |
Markus_Paar | 0:dad421e28e26 | 170 | } |
Markus_Paar | 0:dad421e28e26 | 171 | |
Markus_Paar | 0:dad421e28e26 | 172 | |
Markus_Paar | 0:dad421e28e26 | 173 | /** |
Markus_Paar | 0:dad421e28e26 | 174 | * Reset system. |
Markus_Paar | 0:dad421e28e26 | 175 | */ |
Markus_Paar | 0:dad421e28e26 | 176 | void FirmwareUpdater::reset() { |
Markus_Paar | 0:dad421e28e26 | 177 | mbed_reset(); |
Markus_Paar | 0:dad421e28e26 | 178 | } |
Markus_Paar | 0:dad421e28e26 | 179 | |
Markus_Paar | 0:dad421e28e26 | 180 | /** |
Markus_Paar | 0:dad421e28e26 | 181 | * Fetch a file. |
Markus_Paar | 0:dad421e28e26 | 182 | * |
Markus_Paar | 0:dad421e28e26 | 183 | * @param src_url URL of a source file. |
Markus_Paar | 0:dad421e28e26 | 184 | * @param local_file Local file name. |
Markus_Paar | 0:dad421e28e26 | 185 | * |
Markus_Paar | 0:dad421e28e26 | 186 | * @return Return 0 if it succeed. |
Markus_Paar | 0:dad421e28e26 | 187 | */ |
Markus_Paar | 0:dad421e28e26 | 188 | int FirmwareUpdater::fetch(std::string src_name, std::string local_file) { |
Markus_Paar | 0:dad421e28e26 | 189 | return (0); |
Markus_Paar | 0:dad421e28e26 | 190 | } |
Markus_Paar | 0:dad421e28e26 | 191 | |
Markus_Paar | 0:dad421e28e26 | 192 | |
Markus_Paar | 0:dad421e28e26 | 193 | /** |
Markus_Paar | 0:dad421e28e26 | 194 | * Copy a file. |
Markus_Paar | 0:dad421e28e26 | 195 | * |
Markus_Paar | 0:dad421e28e26 | 196 | * @param local_file1 Source file. |
Markus_Paar | 0:dad421e28e26 | 197 | * @param local_file2 Destination file. |
Markus_Paar | 0:dad421e28e26 | 198 | * |
Markus_Paar | 0:dad421e28e26 | 199 | * @return Return 0 if it succeed. |
Markus_Paar | 0:dad421e28e26 | 200 | */ |
Markus_Paar | 0:dad421e28e26 | 201 | int FirmwareUpdater::copy(std::string local_file1, std::string local_file2) { |
Markus_Paar | 0:dad421e28e26 | 202 | |
Markus_Paar | 0:dad421e28e26 | 203 | LOG("File copying... (%s->%s)\n\r", local_file1.c_str(), local_file2.c_str()); |
Markus_Paar | 0:dad421e28e26 | 204 | |
Markus_Paar | 0:dad421e28e26 | 205 | FILE *rp = fopen(local_file1.c_str(), "rb"); |
Markus_Paar | 0:dad421e28e26 | 206 | if (rp == NULL) { |
Markus_Paar | 0:dad421e28e26 | 207 | LOG("File '%s' open failed.\n", local_file1.c_str()); |
Markus_Paar | 0:dad421e28e26 | 208 | return -1; |
Markus_Paar | 0:dad421e28e26 | 209 | } |
Markus_Paar | 0:dad421e28e26 | 210 | remove(local_file2.c_str()); |
Markus_Paar | 0:dad421e28e26 | 211 | FILE *wp = fopen(local_file2.c_str(), "wb"); |
Markus_Paar | 0:dad421e28e26 | 212 | if (wp == NULL) { |
Markus_Paar | 0:dad421e28e26 | 213 | LOG("File '%s' open failed.\n\r", local_file2.c_str()); |
Markus_Paar | 0:dad421e28e26 | 214 | fclose(rp); |
Markus_Paar | 0:dad421e28e26 | 215 | return -2; |
Markus_Paar | 0:dad421e28e26 | 216 | } |
Markus_Paar | 0:dad421e28e26 | 217 | int c; |
Markus_Paar | 0:dad421e28e26 | 218 | while ((c = fgetc(rp)) != EOF) { |
Markus_Paar | 0:dad421e28e26 | 219 | fputc(c, wp); |
Markus_Paar | 0:dad421e28e26 | 220 | } |
Markus_Paar | 0:dad421e28e26 | 221 | fclose(rp); |
Markus_Paar | 0:dad421e28e26 | 222 | fclose(wp); |
Markus_Paar | 0:dad421e28e26 | 223 | LOG("File copied. (%s->%s)\n\r", local_file1.c_str(), local_file2.c_str()); |
Markus_Paar | 0:dad421e28e26 | 224 | return 0; |
Markus_Paar | 0:dad421e28e26 | 225 | } |
Markus_Paar | 0:dad421e28e26 | 226 | |
Markus_Paar | 0:dad421e28e26 | 227 | |
Markus_Paar | 0:dad421e28e26 | 228 | /** |
Markus_Paar | 0:dad421e28e26 | 229 | * Output a message to a log file. |
Markus_Paar | 0:dad421e28e26 | 230 | * |
Markus_Paar | 0:dad421e28e26 | 231 | * @param format ... |
Markus_Paar | 0:dad421e28e26 | 232 | */ |
Markus_Paar | 0:dad421e28e26 | 233 | void FirmwareUpdater::LOG(const char* format, ...) { |
Markus_Paar | 0:dad421e28e26 | 234 | if (log) { |
Markus_Paar | 0:dad421e28e26 | 235 | FILE *fplog = fopen("/local/update.log", "a"); |
Markus_Paar | 0:dad421e28e26 | 236 | if (fplog != NULL) { |
Markus_Paar | 0:dad421e28e26 | 237 | char buf[BUFSIZ]; |
Markus_Paar | 0:dad421e28e26 | 238 | va_list p; |
Markus_Paar | 0:dad421e28e26 | 239 | va_start(p, format); |
Markus_Paar | 0:dad421e28e26 | 240 | vsnprintf(buf, sizeof(buf) - 1, format, p); |
Markus_Paar | 0:dad421e28e26 | 241 | fprintf(fplog, "%s", buf); |
Markus_Paar | 0:dad421e28e26 | 242 | va_end(p); |
Markus_Paar | 0:dad421e28e26 | 243 | fclose(fplog); |
Markus_Paar | 0:dad421e28e26 | 244 | } |
Markus_Paar | 0:dad421e28e26 | 245 | } |
Markus_Paar | 0:dad421e28e26 | 246 | |
Markus_Paar | 0:dad421e28e26 | 247 | |
Markus_Paar | 0:dad421e28e26 | 248 | } |