Skip to content

Commit

Permalink
tinyusb/msc_fat_view: Add entry for sys/config
Browse files Browse the repository at this point in the history
Signed-off-by: Jerzy Kasenberg <[email protected]>
  • Loading branch information
kasjer committed Apr 26, 2024
1 parent eacf079 commit efd565b
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
7 changes: 7 additions & 0 deletions hw/usb/tinyusb/msc_fat_view/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ pkg.deps:
- "@apache-mynewt-core/kernel/os"
- "@apache-mynewt-core/hw/usb/tinyusb"
- "@apache-mynewt-core/mgmt/imgmgr"
- "@apache-mynewt-core/util/stream"
- "@mcuboot/boot/bootutil"

pkg.deps.MSC_FAT_VIEW_CONFIG:
- "@apache-mynewt-core/sys/config"

pkg.deps.MSC_FAT_VIEW_COREDUMP_FILES:
- "@apache-mynewt-core/sys/coredump"

Expand All @@ -58,6 +62,9 @@ pkg.source_files.MSC_FAT_VIEW_SLOT0_HEX:
pkg.source_files.MSC_FAT_VIEW_MYNEWT_SHORTCUT:
- src/entry_mynewt_htm.c

pkg.source_files.MSC_FAT_VIEW_CONFIG:
- src/entry_config.c

pkg.link_tables:
- msc_fat_view_root_entry

Expand Down
112 changes: 112 additions & 0 deletions hw/usb/tinyusb/msc_fat_view/src/entry_config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <stdint.h>
#include <stream/stream.h>
#include <msc_fat_view/msc_fat_view.h>
#include <modlog/modlog.h>
#include <hal/hal_flash.h>
#include <config/config.h>

struct config_export_stream {
struct out_stream out_stream;
uint8_t *buffer;
uint32_t buffer_start_offset;
uint16_t buffer_end_offset;
uint32_t write_offset;
};

static int
config_export_write(struct out_stream *ostream, const uint8_t *buf, uint32_t count)
{
struct config_export_stream *str = (struct config_export_stream *)ostream;
uint32_t upper_limit = str->write_offset + count;
uint32_t lower_limit = str->write_offset;
int cnt = count;

if (lower_limit < str->buffer_end_offset && upper_limit > str->buffer_start_offset) {
if (lower_limit < str->buffer_start_offset) {
cnt -= str->buffer_start_offset - lower_limit;
lower_limit = str->buffer_start_offset;
}
if (upper_limit > str->buffer_end_offset) {
cnt -= upper_limit - str->buffer_end_offset;
}
memcpy(str->buffer + lower_limit - str->buffer_start_offset,
buf + (lower_limit - str->write_offset), cnt);
}
str->write_offset += count;

return count;
}

static int
config_export_flush(struct out_stream *ostream)
{
return 0;
}

OSTREAM_DEF(config_export);

static struct config_export_stream export_stream = {
.out_stream.vft = &config_export_vft,
};

static void
config_text_export(char *name, char *val)
{
int name_len = strlen(name);
int val_len = 0;
if (val) {
val_len = strlen(val);
}
ostream_write(&export_stream.out_stream, (const uint8_t *)name, name_len, false);
ostream_write(&export_stream.out_stream, (const uint8_t *)" = ", 3, false);
if (val) {
ostream_write(&export_stream.out_stream, (const uint8_t *)val, val_len, false);
}
ostream_write(&export_stream.out_stream, (const uint8_t *)"\n", 1, false);
}

static uint32_t
config_txt_size(const file_entry_t *file_entry)
{
export_stream.buffer = NULL;
export_stream.buffer_start_offset = 0;
export_stream.buffer_end_offset = 0;
export_stream.write_offset = 0;

conf_export(config_text_export, CONF_EXPORT_SHOW);

return export_stream.write_offset;
}

static void
config_txt_read(const struct file_entry *entry, uint32_t file_sector, uint8_t buffer[512])
{
export_stream.buffer = buffer;
export_stream.buffer_start_offset = 512 * file_sector;
export_stream.buffer_end_offset = export_stream.buffer_start_offset + 512;
export_stream.write_offset = 0;

MSC_FAT_VIEW_LOG_DEBUG("Config.txt read %d\n", file_sector);
conf_export(config_text_export, CONF_EXPORT_SHOW);
}

ROOT_DIR_ENTRY(config_txt, "CONFIG.TXT", FAT_FILE_ENTRY_ATTRIBUTE_READ_ONLY, config_txt_size, config_txt_read, NULL, NULL);
5 changes: 5 additions & 0 deletions hw/usb/tinyusb/msc_fat_view/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ syscfg.defs:
Normally this if folder (not file) generated by Windows.
If this file is present Windows will not create folder saving time for writes.
value: 0
MSC_FAT_VIEW_CONFIG:
description: >
If set to 1, root directory will have CONFIG.TXT with content
of sys/config.
value: 0
MSC_FAT_VIEW_AUTO_INSERT:
description: >
If set to 1, media is reported as present during system init.
Expand Down

0 comments on commit efd565b

Please sign in to comment.