2018-04-14 05:28:45 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
implement a file store for embedded firmware images
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AP_ROMFS.h"
|
2018-07-09 03:28:28 -03:00
|
|
|
#include "tinf.h"
|
2021-02-22 22:55:49 -04:00
|
|
|
#include <AP_Math/crc.h>
|
2018-04-14 05:28:45 -03:00
|
|
|
|
2022-02-28 21:19:18 -04:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_HAL/AP_HAL_Boards.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-04-14 05:28:45 -03:00
|
|
|
#ifdef HAL_HAVE_AP_ROMFS_EMBEDDED_H
|
|
|
|
#include <ap_romfs_embedded.h>
|
|
|
|
#else
|
|
|
|
const AP_ROMFS::embedded_file AP_ROMFS::files[] = {};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
find an embedded file
|
|
|
|
*/
|
2023-12-30 15:53:15 -04:00
|
|
|
const AP_ROMFS::embedded_file *AP_ROMFS::find_file(const char *name)
|
2018-04-14 05:28:45 -03:00
|
|
|
{
|
2018-08-02 20:27:17 -03:00
|
|
|
for (uint16_t i=0; i<ARRAY_SIZE(files); i++) {
|
2018-04-14 05:28:45 -03:00
|
|
|
if (strcmp(name, files[i].filename) == 0) {
|
2023-12-30 15:53:15 -04:00
|
|
|
return &files[i];
|
2018-04-14 05:28:45 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-09 03:28:28 -03:00
|
|
|
|
|
|
|
/*
|
2024-01-24 14:16:01 -04:00
|
|
|
find a compressed file and uncompress it. Space for decompressed data comes
|
|
|
|
from malloc. Caller must be careful to free the resulting data after use. The
|
|
|
|
file data buffer is guaranteed to contain at least one null (though it may be
|
|
|
|
at buf[size]).
|
2018-07-09 03:28:28 -03:00
|
|
|
*/
|
2019-10-23 06:54:32 -03:00
|
|
|
const uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size)
|
2018-07-09 03:28:28 -03:00
|
|
|
{
|
2023-12-30 15:53:15 -04:00
|
|
|
const struct embedded_file *f = find_file(name);
|
|
|
|
if (!f) {
|
2018-07-09 03:28:28 -03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-23 06:54:32 -03:00
|
|
|
#ifdef HAL_ROMFS_UNCOMPRESSED
|
2024-01-24 14:16:01 -04:00
|
|
|
size = f->decompressed_size;
|
2023-12-30 15:53:15 -04:00
|
|
|
return f->contents;
|
2019-10-23 06:54:32 -03:00
|
|
|
#else
|
2023-12-30 15:53:15 -04:00
|
|
|
uint8_t *decompressed_data = (uint8_t *)malloc(f->decompressed_size+1);
|
2018-07-09 03:28:28 -03:00
|
|
|
if (!decompressed_data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-02-20 21:58:08 -04:00
|
|
|
if (f->decompressed_size == 0) {
|
|
|
|
// empty file
|
|
|
|
size = 0;
|
|
|
|
return decompressed_data;
|
|
|
|
}
|
|
|
|
|
2024-01-24 14:16:01 -04:00
|
|
|
// explicitly null-terminate the data
|
2023-12-30 15:53:15 -04:00
|
|
|
decompressed_data[f->decompressed_size] = 0;
|
2018-10-29 19:17:12 -03:00
|
|
|
|
2018-07-09 03:28:28 -03:00
|
|
|
TINF_DATA *d = (TINF_DATA *)malloc(sizeof(TINF_DATA));
|
|
|
|
if (!d) {
|
2019-10-23 06:54:32 -03:00
|
|
|
::free(decompressed_data);
|
2018-07-09 03:28:28 -03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
uzlib_uncompress_init(d, NULL, 0);
|
|
|
|
|
2023-12-30 15:53:15 -04:00
|
|
|
d->source = f->contents;
|
|
|
|
d->source_limit = f->contents + f->compressed_size;
|
2018-07-09 03:28:28 -03:00
|
|
|
d->dest = decompressed_data;
|
2023-12-30 15:53:15 -04:00
|
|
|
d->destSize = f->decompressed_size;
|
2018-07-09 03:28:28 -03:00
|
|
|
|
2024-01-15 17:57:28 -04:00
|
|
|
int res = uzlib_uncompress(d);
|
2018-07-09 03:28:28 -03:00
|
|
|
|
2019-10-23 06:54:32 -03:00
|
|
|
::free(d);
|
2018-07-09 03:28:28 -03:00
|
|
|
|
|
|
|
if (res != TINF_OK) {
|
2019-10-23 06:54:32 -03:00
|
|
|
::free(decompressed_data);
|
2018-07-09 03:28:28 -03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-12-30 15:53:15 -04:00
|
|
|
if (crc32_small(0, decompressed_data, f->decompressed_size) != f->crc) {
|
2021-02-22 22:55:49 -04:00
|
|
|
::free(decompressed_data);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-12-30 15:53:15 -04:00
|
|
|
size = f->decompressed_size;
|
2018-07-09 03:28:28 -03:00
|
|
|
return decompressed_data;
|
2019-10-23 06:54:32 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// free returned data
|
|
|
|
void AP_ROMFS::free(const uint8_t *data)
|
|
|
|
{
|
|
|
|
#ifndef HAL_ROMFS_UNCOMPRESSED
|
|
|
|
::free(const_cast<uint8_t *>(data));
|
|
|
|
#endif
|
2018-07-09 03:28:28 -03:00
|
|
|
}
|
2020-03-06 20:53:39 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
directory listing interface. Start with ofs=0. Returns pathnames
|
|
|
|
that match dirname prefix. Ends with nullptr return when no more
|
|
|
|
files found
|
|
|
|
*/
|
|
|
|
const char *AP_ROMFS::dir_list(const char *dirname, uint16_t &ofs)
|
|
|
|
{
|
|
|
|
const size_t dlen = strlen(dirname);
|
|
|
|
for ( ; ofs < ARRAY_SIZE(files); ofs++) {
|
2024-02-20 21:58:08 -04:00
|
|
|
if (strncmp(dirname, files[ofs].filename, dlen) == 0) {
|
|
|
|
const char last_char = files[ofs].filename[dlen];
|
|
|
|
if (dlen != 0 && last_char != '/' && last_char != 0) {
|
|
|
|
// only a partial match, skip
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
prevent duplicate directories
|
|
|
|
*/
|
|
|
|
const char *start_name = files[ofs].filename + dlen + 1;
|
|
|
|
const char *slash = strchr(start_name, '/');
|
|
|
|
if (ofs > 0 && slash != nullptr) {
|
|
|
|
auto len = slash - start_name;
|
|
|
|
if (memcmp(files[ofs].filename, files[ofs-1].filename, len+dlen+1) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 20:53:39 -04:00
|
|
|
// found one
|
|
|
|
return files[ofs++].filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|