|
34 | 34 | #include <modlog/modlog.h>
|
35 | 35 | #include <hal/hal_flash.h>
|
36 | 36 | #include <console/console.h>
|
| 37 | +#include <stream/stream.h> |
37 | 38 | #include "coredump_files.h"
|
38 | 39 |
|
39 | 40 | #if MYNEWT_VAL(BOOT_LOADER)
|
|
76 | 77 | #define SECTOR_BIT_COUNT ((SECTOR_SIZE) * 8)
|
77 | 78 |
|
78 | 79 | #define DIR_ENTRY_SIZE 32
|
79 |
| -#define ROOT_SECTOR_COUNT 1 |
| 80 | +#define ROOT_SECTOR_COUNT MYNEWT_VAL(MSC_FAT_VIEW_ROOT_DIR_SECTORS) |
80 | 81 |
|
81 | 82 | #define FAT_FIRST_SECTOR 1
|
82 | 83 | #define FAT_ROOT_DIR_FIRST_SECTOR ((FAT_FIRST_SECTOR) + (FAT_SECTOR_COUNT) * (FAT_COUNT))
|
@@ -787,23 +788,20 @@ msc_fat_view_fat_next_cluster(cluster_t cluster, fat_chain_t **cache)
|
787 | 788 | }
|
788 | 789 |
|
789 | 790 | static void
|
790 |
| -msc_fat_view_write_ucs_2(uint8_t *dst, const char **ascii, int len) |
| 791 | +msc_fat_view_write_ucs_2(struct out_stream *ostr, const char *ascii, int field_len) |
791 | 792 | {
|
792 |
| - for ( ; len; --len) { |
793 |
| - if (*ascii) { |
794 |
| - if (**ascii) { |
795 |
| - *dst++ = **ascii; |
796 |
| - *ascii += 1; |
797 |
| - } else { |
798 |
| - *dst++ = 0; |
799 |
| - *ascii = NULL; |
800 |
| - } |
801 |
| - *dst++ = 0; |
| 793 | + for (; ascii && field_len; --field_len) { |
| 794 | + ostream_write_uint8(ostr, *ascii); |
| 795 | + ostream_write_uint8(ostr, 0); |
| 796 | + if (*ascii == '\0') { |
| 797 | + ascii = NULL; |
802 | 798 | } else {
|
803 |
| - *dst++ = 0xFF; |
804 |
| - *dst++ = 0xFF; |
| 799 | + ++ascii; |
805 | 800 | }
|
806 | 801 | }
|
| 802 | + for (; field_len; --field_len) { |
| 803 | + ostream_write_uint16(ostr, 0xFFFF); |
| 804 | + } |
807 | 805 | }
|
808 | 806 |
|
809 | 807 | static void
|
@@ -832,34 +830,46 @@ msc_fat_view_short_name_checksum(const char *short_name)
|
832 | 830 | return check_sum;
|
833 | 831 | }
|
834 | 832 |
|
835 |
| -static fat_dir_entry_t * |
836 |
| -msc_fat_view_write_long_name_entry(fat_dir_entry_t *buffer, const char *long_name, const char *short_name) |
| 833 | +/* |
| 834 | + * Return pointer to character in the string if offset is valid |
| 835 | + * If offset is outside of the string return NULL |
| 836 | + */ |
| 837 | +static const char * |
| 838 | +long_name_part(const char *long_name, int long_name_len, int long_name_offset) |
| 839 | +{ |
| 840 | + if (long_name_offset > long_name_len + 1) { |
| 841 | + return NULL; |
| 842 | + } |
| 843 | + return long_name + long_name_offset; |
| 844 | +} |
| 845 | + |
| 846 | +void |
| 847 | +msc_fat_view_write_long_name_entry(struct out_stream *ostr, const char *name, |
| 848 | + const char *short_name) |
837 | 849 | {
|
838 | 850 | int len;
|
839 | 851 | int n;
|
840 | 852 | int i;
|
841 | 853 | uint8_t checksum;
|
842 |
| - fat_dir_entry_t *p; |
| 854 | + int offset; |
843 | 855 |
|
844 |
| - len = strlen(long_name); |
| 856 | + len = strlen(name); |
845 | 857 | if (len) {
|
846 | 858 | checksum = msc_fat_view_short_name_checksum(short_name);
|
847 | 859 | n = (len + 12) / 13;
|
848 |
| - buffer += n; |
849 |
| - for (i = 1; i <= n; ++i) { |
850 |
| - p = buffer - i; |
| 860 | + for (i = n; i > 0; --i) { |
| 861 | + offset = (i - 1) * 13; |
851 | 862 | /* First part of long entry has the highest index and 6th bit set */
|
852 |
| - p->sequence = i + ((i == n) ? 0x40 : 0); |
853 |
| - p->attr1 = 0x0f; |
854 |
| - p->reserved2 = 0; |
855 |
| - p->reserved3 = 0; |
856 |
| - p->checksum = checksum; |
857 |
| - msc_fat_view_write_ucs_2((uint8_t *)p->name1, &long_name, 5); |
858 |
| - msc_fat_view_write_ucs_2((uint8_t *)p->name2, &long_name, 6); |
859 |
| - msc_fat_view_write_ucs_2((uint8_t *)p->name3, &long_name, 2); |
| 863 | + ostream_write_uint8(ostr, i + ((i == n) ? 0x40 : 0)); |
| 864 | + msc_fat_view_write_ucs_2(ostr, long_name_part(name, len, offset), 5); |
| 865 | + ostream_write_uint8(ostr, 0x0f); /* attrib */ |
| 866 | + ostream_write_uint8(ostr, 0); /* reserved2 */ |
| 867 | + ostream_write_uint8(ostr, checksum); |
| 868 | + msc_fat_view_write_ucs_2(ostr, long_name_part(name, len, offset + 5), 6); |
| 869 | + ostream_write_uint16(ostr, 0); /* reserved3 */ |
| 870 | + msc_fat_view_write_ucs_2(ostr, long_name_part(name, len, offset + 11), 2); |
860 | 871 | }
|
861 | 872 | }
|
862 |
| - return buffer; |
863 | 873 | }
|
864 | 874 |
|
865 | 875 | static char *
|
@@ -958,35 +968,41 @@ msc_fat_view_read_root_sector(uint16_t dir_sector, uint8_t buffer[512])
|
958 | 968 | int cluster = 2;
|
959 | 969 | int cluster_count;
|
960 | 970 | uint32_t size;
|
961 |
| - fat_dir_entry_t *dst = (fat_dir_entry_t *)buffer; |
| 971 | + struct mem_out_stream mstr; |
| 972 | + struct out_stream *ostr = (struct out_stream *)&mstr; |
| 973 | + fat_dir_entry_t fat_dir_entry; |
962 | 974 | dir_entry_t *entry;
|
963 | 975 | char short_name[11];
|
964 | 976 |
|
| 977 | + memset(buffer, 0, 512); |
| 978 | + mem_ostream_init(&mstr, buffer, 512); |
| 979 | + mstr.write_ptr -= dir_sector * 512; |
| 980 | + |
965 | 981 | TU_LOG1("msc_fat_view_read_root %d\n", dir_sector);
|
966 | 982 | for (i = 0; i < root_dir_entry_count; ++i) {
|
967 | 983 | entry = &root_dir[i];
|
968 | 984 | msc_fat_view_create_short_name(entry, short_name);
|
969 | 985 | if (entry->dir_slots > 1) {
|
970 |
| - dst = msc_fat_view_write_long_name_entry(dst, entry->file->name, short_name); |
| 986 | + msc_fat_view_write_long_name_entry(ostr, entry->file->name, short_name); |
971 | 987 | }
|
972 |
| - memcpy(dst->name, short_name, 11); |
| 988 | + memcpy(fat_dir_entry.bytes, short_name, 11); |
973 | 989 | /* Clear out rest of the entry */
|
974 |
| - memset(dst->bytes + 11, 0, 32 - 11); |
975 |
| - dst->attr = root_dir[i].file->attributes; |
| 990 | + memset(fat_dir_entry.bytes + 11, 0, 32 - 11); |
| 991 | + |
| 992 | + fat_dir_entry.attr = root_dir[i].file->attributes; |
976 | 993 | if (i > 0) {
|
977 | 994 | assert(root_dir[i].file->size);
|
978 | 995 | size = root_dir[i].file->size(root_dir[i].file);
|
979 | 996 | if (size) {
|
980 | 997 | cluster_count = cluster_count_from_bytes(size);
|
981 |
| - dst->cluster_hi = htole16((cluster >> 16)); |
982 |
| - dst->cluster_lo = htole16((uint16_t)cluster); |
983 |
| - dst->size = htole32(size); |
| 998 | + fat_dir_entry.cluster_hi = htole16((cluster >> 16)); |
| 999 | + fat_dir_entry.cluster_lo = htole16((uint16_t)cluster); |
| 1000 | + fat_dir_entry.size = htole32(size); |
984 | 1001 | cluster += cluster_count;
|
985 | 1002 | }
|
986 | 1003 | }
|
987 |
| - dst++; |
| 1004 | + ostream_write(ostr, fat_dir_entry.bytes, 32, false); |
988 | 1005 | }
|
989 |
| - memset(dst, 0xE5, buffer + 512 - (uint8_t *)dst); |
990 | 1006 | }
|
991 | 1007 |
|
992 | 1008 | static void
|
|
0 commit comments