Changes in / [8cc4ddb:1a5b252] in mainline
- Files:
-
- 1 added
- 22 deleted
- 46 edited
Legend:
- Unmodified
- Added
- Removed
-
abi/include/syscall.h
r8cc4ddb r1a5b252 89 89 SYS_UNREGISTER_IRQ, 90 90 91 SYS_SYSINFO_GET_ VAL_TYPE,91 SYS_SYSINFO_GET_TAG, 92 92 SYS_SYSINFO_GET_VALUE, 93 93 SYS_SYSINFO_GET_DATA_SIZE, -
abi/include/sysinfo.h
r8cc4ddb r1a5b252 37 37 #define ABI_SYSINFO_H_ 38 38 39 #include <abi/proc/task.h>40 #include <abi/proc/thread.h>41 42 39 /** Number of load components */ 43 40 #define LOAD_STEPS 3 … … 47 44 #define EXC_NAME_BUFLEN 20 48 45 49 /** Item value type 50 * 51 */ 46 /** Thread states */ 52 47 typedef enum { 53 SYSINFO_VAL_UNDEFINED = 0, /**< Undefined value */ 54 SYSINFO_VAL_VAL = 1, /**< Constant numeric value */ 55 SYSINFO_VAL_DATA = 2, /**< Constant binary data */ 56 SYSINFO_VAL_FUNCTION_VAL = 3, /**< Generated numeric value */ 57 SYSINFO_VAL_FUNCTION_DATA = 4 /**< Generated binary data */ 58 } sysinfo_item_val_type_t; 48 /** It is an error, if thread is found in this state. */ 49 Invalid, 50 /** State of a thread that is currently executing on some CPU. */ 51 Running, 52 /** Thread in this state is waiting for an event. */ 53 Sleeping, 54 /** State of threads in a run queue. */ 55 Ready, 56 /** Threads are in this state before they are first readied. */ 57 Entering, 58 /** After a thread calls thread_exit(), it is put into Exiting state. */ 59 Exiting, 60 /** Threads that were not detached but exited are Lingering. */ 61 Lingering 62 } state_t; 59 63 60 64 /** Statistics about a single CPU -
boot/Makefile.common
r8cc4ddb r1a5b252 102 102 $(USPACE_PATH)/srv/fs/tmpfs/tmpfs \ 103 103 $(USPACE_PATH)/srv/fs/fat/fat \ 104 $(USPACE_PATH)/srv/fs/exfat/exfat \105 104 $(USPACE_PATH)/srv/fs/ext2fs/ext2fs \ 106 105 $(USPACE_PATH)/srv/taskmon/taskmon \ -
boot/generic/src/printf_core.c
r8cc4ddb r1a5b252 70 70 */ 71 71 #define PRINT_NUMBER_BUFFER_SIZE (64 + 5) 72 73 /** Get signed or unsigned integer argument */74 #define PRINTF_GET_INT_ARGUMENT(type, ap, flags) \75 ({ \76 unsigned type res; \77 \78 if ((flags) & __PRINTF_FLAG_SIGNED) { \79 signed type arg = va_arg((ap), signed type); \80 \81 if (arg < 0) { \82 res = -arg; \83 (flags) |= __PRINTF_FLAG_NEGATIVE; \84 } else \85 res = arg; \86 } else \87 res = va_arg((ap), unsigned type); \88 \89 res; \90 })91 72 92 73 /** Enumeration of possible arguments types. … … 190 171 } 191 172 192 return (int) (counter );173 return (int) (counter + 1); 193 174 } 194 175 … … 717 698 size_t size; 718 699 uint64_t number; 719 720 700 switch (qualifier) { 721 701 case PrintfQualifierByte: 722 702 size = sizeof(unsigned char); 723 number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);703 number = (uint64_t) va_arg(ap, unsigned int); 724 704 break; 725 705 case PrintfQualifierShort: 726 706 size = sizeof(unsigned short); 727 number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);707 number = (uint64_t) va_arg(ap, unsigned int); 728 708 break; 729 709 case PrintfQualifierInt: 730 710 size = sizeof(unsigned int); 731 number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);711 number = (uint64_t) va_arg(ap, unsigned int); 732 712 break; 733 713 case PrintfQualifierLong: 734 714 size = sizeof(unsigned long); 735 number = PRINTF_GET_INT_ARGUMENT(long, ap, flags);715 number = (uint64_t) va_arg(ap, unsigned long); 736 716 break; 737 717 case PrintfQualifierLongLong: 738 718 size = sizeof(unsigned long long); 739 number = PRINTF_GET_INT_ARGUMENT(long long, ap, flags);719 number = (uint64_t) va_arg(ap, unsigned long long); 740 720 break; 741 721 case PrintfQualifierPointer: … … 752 732 counter = -counter; 753 733 goto out; 734 } 735 736 if (flags & __PRINTF_FLAG_SIGNED) { 737 if (number & (0x1 << (size * 8 - 1))) { 738 flags |= __PRINTF_FLAG_NEGATIVE; 739 740 if (size == sizeof(uint64_t)) { 741 number = -((int64_t) number); 742 } else { 743 number = ~number; 744 number &= 745 ~(0xFFFFFFFFFFFFFFFFll << 746 (size * 8)); 747 number++; 748 } 749 } 754 750 } 755 751 -
kernel/arch/amd64/include/asm.h
r8cc4ddb r1a5b252 41 41 #include <trace.h> 42 42 43 #define IO_SPACE_BOUNDARY ((void *) (64 * 1024))44 45 43 /** Return base address of current stack. 46 44 * … … 79 77 } 80 78 79 #define IO_SPACE_BOUNDARY ((void *) (64 * 1024)) 80 81 81 /** Byte from port 82 82 * … … 91 91 if (port < (ioport8_t *) IO_SPACE_BOUNDARY) { 92 92 uint8_t val; 93 94 93 asm volatile ( 95 94 "inb %w[port], %b[val]\n" … … 97 96 : [port] "d" (port) 98 97 ); 99 100 98 return val; 101 } else 99 } else { 102 100 return (uint8_t) *port; 101 } 103 102 } 104 103 … … 115 114 if (port < (ioport16_t *) IO_SPACE_BOUNDARY) { 116 115 uint16_t val; 117 118 116 asm volatile ( 119 117 "inw %w[port], %w[val]\n" … … 121 119 : [port] "d" (port) 122 120 ); 123 124 121 return val; 125 } else 122 } else { 126 123 return (uint16_t) *port; 124 } 127 125 } 128 126 … … 139 137 if (port < (ioport32_t *) IO_SPACE_BOUNDARY) { 140 138 uint32_t val; 141 142 139 asm volatile ( 143 140 "inl %w[port], %[val]\n" … … 145 142 : [port] "d" (port) 146 143 ); 147 148 144 return val; 149 } else 145 } else { 150 146 return (uint32_t) *port; 147 } 151 148 } 152 149 … … 166 163 :: [val] "a" (val), [port] "d" (port) 167 164 ); 168 } else 165 } else { 169 166 *port = val; 167 } 170 168 } 171 169 … … 185 183 :: [val] "a" (val), [port] "d" (port) 186 184 ); 187 } else 185 } else { 188 186 *port = val; 187 } 189 188 } 190 189 … … 204 203 :: [val] "a" (val), [port] "d" (port) 205 204 ); 206 } else 205 } else { 207 206 *port = val; 207 } 208 208 } 209 209 -
kernel/arch/ia32/include/asm.h
r8cc4ddb r1a5b252 118 118 :: [val] "a" (val), [port] "d" (port) 119 119 ); 120 } else 120 } else { 121 121 *port = val; 122 } 122 123 } 123 124 … … 137 138 :: [val] "a" (val), [port] "d" (port) 138 139 ); 139 } else 140 } else { 140 141 *port = val; 142 } 141 143 } 142 144 … … 156 158 :: [val] "a" (val), [port] "d" (port) 157 159 ); 158 } else 160 } else { 159 161 *port = val; 162 } 160 163 } 161 164 … … 172 175 if (((void *)port) < IO_SPACE_BOUNDARY) { 173 176 uint8_t val; 174 175 177 asm volatile ( 176 178 "inb %w[port], %b[val]\n" … … 178 180 : [port] "d" (port) 179 181 ); 180 181 182 return val; 182 } else 183 } else { 183 184 return (uint8_t) *port; 185 } 184 186 } 185 187 … … 196 198 if (((void *)port) < IO_SPACE_BOUNDARY) { 197 199 uint16_t val; 198 199 200 asm volatile ( 200 201 "inw %w[port], %w[val]\n" … … 202 203 : [port] "d" (port) 203 204 ); 204 205 205 return val; 206 } else 206 } else { 207 207 return (uint16_t) *port; 208 } 208 209 } 209 210 … … 220 221 if (((void *)port) < IO_SPACE_BOUNDARY) { 221 222 uint32_t val; 222 223 223 asm volatile ( 224 224 "inl %w[port], %[val]\n" … … 226 226 : [port] "d" (port) 227 227 ); 228 229 228 return val; 230 } else 229 } else { 231 230 return (uint32_t) *port; 231 } 232 232 } 233 233 -
kernel/generic/include/ipc/ipc.h
r8cc4ddb r1a5b252 36 36 #define KERN_IPC_H_ 37 37 38 #include <abi/ipc/ipc.h> 38 39 #include <synch/spinlock.h> 39 40 #include <synch/mutex.h> 40 41 #include <synch/waitq.h> 41 #include <abi/ipc/ipc.h>42 #include <abi/proc/task.h>43 42 #include <typedefs.h> 44 43 45 #define IPC_MAX_PHONES 6444 #define IPC_MAX_PHONES 32 46 45 47 46 struct answerbox; -
kernel/generic/include/sysinfo/sysinfo.h
r8cc4ddb r1a5b252 38 38 #include <typedefs.h> 39 39 #include <str.h> 40 #include <abi/sysinfo.h>41 40 42 41 /** Framebuffer info exported flags */ 43 42 extern bool fb_exported; 43 44 /** Item value type 45 * 46 */ 47 typedef enum { 48 SYSINFO_VAL_UNDEFINED = 0, /**< Undefined value */ 49 SYSINFO_VAL_VAL = 1, /**< Constant numeric value */ 50 SYSINFO_VAL_DATA = 2, /**< Constant binary data */ 51 SYSINFO_VAL_FUNCTION_VAL = 3, /**< Generated numeric value */ 52 SYSINFO_VAL_FUNCTION_DATA = 4 /**< Generated binary data */ 53 } sysinfo_item_val_type_t; 44 54 45 55 /** Subtree type … … 135 145 extern void sysinfo_dump(sysinfo_item_t *); 136 146 137 extern sysarg_t sys_sysinfo_get_ val_type(void *, size_t);147 extern sysarg_t sys_sysinfo_get_tag(void *, size_t); 138 148 extern sysarg_t sys_sysinfo_get_value(void *, size_t, void *); 139 149 extern sysarg_t sys_sysinfo_get_data_size(void *, size_t, void *); -
kernel/generic/include/typedefs.h
r8cc4ddb r1a5b252 39 39 #include <arch/common.h> 40 40 #include <arch/types.h> 41 #include <abi/bool.h>42 41 43 42 #define NULL ((void *) 0) … … 62 61 typedef void (* function)(); 63 62 63 typedef uint8_t bool; 64 typedef uint64_t thread_id_t; 65 typedef uint64_t task_id_t; 64 66 typedef uint32_t container_id_t; 65 67 -
kernel/generic/src/mm/slab.c
r8cc4ddb r1a5b252 180 180 unsigned int flags) 181 181 { 182 183 182 184 size_t zone = 0; 183 185 -
kernel/generic/src/printf/printf_core.c
r8cc4ddb r1a5b252 75 75 #define PRINT_NUMBER_BUFFER_SIZE (64 + 5) 76 76 77 /** Get signed or unsigned integer argument */78 #define PRINTF_GET_INT_ARGUMENT(type, ap, flags) \79 ({ \80 unsigned type res; \81 \82 if ((flags) & __PRINTF_FLAG_SIGNED) { \83 signed type arg = va_arg((ap), signed type); \84 \85 if (arg < 0) { \86 res = -arg; \87 (flags) |= __PRINTF_FLAG_NEGATIVE; \88 } else \89 res = arg; \90 } else \91 res = va_arg((ap), unsigned type); \92 \93 res; \94 })95 96 77 /** Enumeration of possible arguments types. 97 78 */ … … 226 207 } 227 208 228 return (int) (counter );209 return (int) (counter + 1); 229 210 } 230 211 … … 264 245 } 265 246 266 return (int) (counter );247 return (int) (counter + 1); 267 248 } 268 249 … … 851 832 size_t size; 852 833 uint64_t number; 853 854 834 switch (qualifier) { 855 835 case PrintfQualifierByte: 856 836 size = sizeof(unsigned char); 857 number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);837 number = (uint64_t) va_arg(ap, unsigned int); 858 838 break; 859 839 case PrintfQualifierShort: 860 840 size = sizeof(unsigned short); 861 number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);841 number = (uint64_t) va_arg(ap, unsigned int); 862 842 break; 863 843 case PrintfQualifierInt: 864 844 size = sizeof(unsigned int); 865 number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);845 number = (uint64_t) va_arg(ap, unsigned int); 866 846 break; 867 847 case PrintfQualifierLong: 868 848 size = sizeof(unsigned long); 869 number = PRINTF_GET_INT_ARGUMENT(long, ap, flags);849 number = (uint64_t) va_arg(ap, unsigned long); 870 850 break; 871 851 case PrintfQualifierLongLong: 872 852 size = sizeof(unsigned long long); 873 number = PRINTF_GET_INT_ARGUMENT(long long, ap, flags);853 number = (uint64_t) va_arg(ap, unsigned long long); 874 854 break; 875 855 case PrintfQualifierPointer: … … 886 866 counter = -counter; 887 867 goto out; 868 } 869 870 if (flags & __PRINTF_FLAG_SIGNED) { 871 if (number & (0x1 << (size * 8 - 1))) { 872 flags |= __PRINTF_FLAG_NEGATIVE; 873 874 if (size == sizeof(uint64_t)) { 875 number = -((int64_t) number); 876 } else { 877 number = ~number; 878 number &= 879 ~(0xFFFFFFFFFFFFFFFFll << 880 (size * 8)); 881 number++; 882 } 883 } 888 884 } 889 885 -
kernel/generic/src/syscall/syscall.c
r8cc4ddb r1a5b252 181 181 182 182 /* Sysinfo syscalls. */ 183 (syshandler_t) sys_sysinfo_get_ val_type,183 (syshandler_t) sys_sysinfo_get_tag, 184 184 (syshandler_t) sys_sysinfo_get_value, 185 185 (syshandler_t) sys_sysinfo_get_data_size, -
kernel/generic/src/sysinfo/sysinfo.c
r8cc4ddb r1a5b252 661 661 * 662 662 */ 663 sysarg_t sys_sysinfo_get_ val_type(void *path_ptr, size_t path_size)663 sysarg_t sys_sysinfo_get_tag(void *path_ptr, size_t path_size) 664 664 { 665 665 /* -
kernel/test/print/print2.c
r8cc4ddb r1a5b252 44 44 TPRINTF("Real output: [%d] [%3.2d] [%-3.2d] [%2.3d] [%-2.3d]\n\n", -1, -2, -3, -4, -5); 45 45 46 TPRINTF("Testing printf(\"%%lld %%3.2lld %%-3.2lld %%2.3lld %%-2.3lld\", (long long) -1, (long long) -2, (long long) -3, (long long) -4, (long long) -5):\n");47 TPRINTF("Expected output: [-1] [-02] [-03] [-004] [-005]\n");48 TPRINTF("Real output: [%lld] [%3.2lld] [%-3.2lld] [%2.3lld] [%-2.3lld]\n\n", (long long) -1, (long long) -2, (long long) -3, (long long) -4, (long long) -5);49 50 46 TPRINTF("Testing printf(\"%%#x %%5.3#x %%-5.3#x %%3.5#x %%-3.5#x\", 17, 18, 19, 20, 21):\n"); 51 47 TPRINTF("Expected output: [0x11] [0x012] [0x013] [0x00014] [0x00015]\n"); -
uspace/Makefile
r8cc4ddb r1a5b252 79 79 srv/bd/part/guid_part \ 80 80 srv/bd/part/mbr_part \ 81 srv/fs/exfat \82 81 srv/fs/fat \ 83 82 srv/fs/tmpfs \ … … 164 163 lib/block \ 165 164 lib/clui \ 166 lib/fmtutil \167 165 lib/scsi \ 168 166 lib/softint \ -
uspace/Makefile.common
r8cc4ddb r1a5b252 108 108 LIBIMGMAP_PREFIX = $(LIB_PREFIX)/imgmap 109 109 LIBCLUI_PREFIX = $(LIB_PREFIX)/clui 110 LIBFMTUTIL_PREFIX = $(LIB_PREFIX)/fmtutil111 110 112 111 LIBEXT2_PREFIX = $(LIB_PREFIX)/ext2 -
uspace/app/bdsh/Makefile
r8cc4ddb r1a5b252 29 29 30 30 USPACE_PREFIX = ../.. 31 LIBS = $(LIBBLOCK_PREFIX)/libblock.a $(LIBCLUI_PREFIX)/libclui.a \ 32 $(LIBFMTUTIL_PREFIX)/libfmtutil.a 33 EXTRA_CFLAGS = -I$(LIBBLOCK_PREFIX) -I$(LIBCLUI_PREFIX) \ 34 -I$(LIBFMTUTIL_PREFIX) -I. -Icmds/ -Icmds/builtins -Icmds/modules 31 LIBS = $(LIBBLOCK_PREFIX)/libblock.a $(LIBCLUI_PREFIX)/libclui.a 32 EXTRA_CFLAGS = -I$(LIBBLOCK_PREFIX) -I$(LIBCLUI_PREFIX) -I. -Icmds/ \ 33 -Icmds/builtins -Icmds/modules 35 34 BINARY = bdsh 36 35 -
uspace/app/bdsh/cmds/modules/help/help.c
r8cc4ddb r1a5b252 1 1 /* 2 2 * Copyright (c) 2008 Tim Post 3 * Copyright (c) 2011 Martin Sucha4 3 * All rights reserved. 5 4 * … … 31 30 #include <stdlib.h> 32 31 #include <str.h> 33 #include <fmtutil.h>34 32 35 33 #include "config.h" … … 130 128 static void help_survival(void) 131 129 { 132 print_wrapped_console( 133 "Don't panic!\n\n" 130 printf("Don't panic!\n\n"); 134 131 135 132 printf("This is Bdsh, the Brain dead shell, currently " 136 133 "the primary user interface to HelenOS. Bdsh allows you to enter " 137 134 "commands and supports history (Up, Down arrow keys), " 138 135 "line editing (Left Arrow, Right Arrow, Home, End, Backspace), " 139 136 "selection (Shift + movement keys), copy and paste (Ctrl-C, " 140 "Ctrl-V), similar to common desktop environments.\n\n" 137 "Ctrl-V), similar to common desktop environments.\n\n"); 141 138 142 139 printf("The most basic filesystem commands are Bdsh builtins. Type " 143 140 "'help commands' [Enter] to see the list of Bdsh builtin commands. " 144 141 "Other commands are external executables located in the /app and " 145 142 "/srv directories. Type 'ls /app' [Enter] and 'ls /srv' [Enter] " 146 143 "to see their list. You can execute an external command simply " 147 "by entering its name (e.g. type 'tetris' [Enter]).\n\n" 144 "by entering its name (e.g. type 'tetris' [Enter]).\n\n"); 148 145 149 150 "these using the F1-F11 keys.\n\n" 146 printf("HelenOS has virtual consoles (VCs). You can switch between " 147 "these using the F1-F11 keys.\n\n"); 151 148 152 149 printf("This is but a small glimpse of what you can do with HelenOS. " 153 150 "To learn more please point your browser to the HelenOS User's " 154 "Guide: http://trac.helenos.org/wiki/UsersGuide\n\n", 155 ALIGN_LEFT); 151 "Guide: http://trac.helenos.org/trac.fcgi/wiki/UsersGuide\n\n"); 156 152 } 157 153 -
uspace/app/bdsh/compl.c
r8cc4ddb r1a5b252 90 90 { 91 91 compl_t *cs = NULL; 92 size_t pref_size; 92 93 char *stext = NULL; 93 94 char *prefix = NULL; 94 95 char *dirname = NULL; 95 int retval;96 97 token_t *tokens = calloc(WORD_MAX, sizeof(token_t));98 if (tokens == NULL) {99 retval = ENOMEM;100 goto error;101 }102 103 size_t pref_size;104 96 char *rpath_sep; 105 97 static const char *dirlist_arg[] = { ".", NULL }; 98 int retval; 106 99 tokenizer_t tok; 107 ssize_t current_token; 100 token_t tokens[WORD_MAX]; 101 unsigned int current_token; 108 102 size_t tokens_length; 109 103 110 104 cs = calloc(1, sizeof(compl_t)); 111 105 if (!cs) { … … 113 107 goto error; 114 108 } 115 109 116 110 /* Convert text buffer to string */ 117 111 stext = wstr_to_astr(text); … … 133 127 134 128 /* Find the current token */ 135 for (current_token = 0; current_token < (ssize_t) tokens_length; 136 current_token++) { 129 for (current_token = 0; current_token < tokens_length; current_token++) { 137 130 token_t *t = &tokens[current_token]; 138 131 size_t end = t->char_start + t->char_length; 139 140 /* 141 * Check if the caret lies inside the token or immediately 132 /* Check if the caret lies inside the token or immediately 142 133 * after it 143 134 */ … … 147 138 } 148 139 149 if (tokens_length == 0) 150 current_token = -1; 151 152 if ((current_token >= 0) && (tokens[current_token].type != TOKTYPE_SPACE)) 140 if (tokens[current_token].type != TOKTYPE_SPACE) { 153 141 *cstart = tokens[current_token].char_start; 154 else 142 } 143 else { 155 144 *cstart = pos; 156 157 /*158 145 } 146 147 /* Extract the prefix being completed 159 148 * XXX: handle strings, etc. 160 149 */ … … 165 154 goto error; 166 155 } 167 prefix[pref_size] = 0; 168 169 if (current_token >= 0) { 170 str_ncpy(prefix, pref_size + 1, stext + 171 tokens[current_token].byte_start, pref_size); 172 } 156 157 str_ncpy(prefix, pref_size + 1, stext + 158 tokens[current_token].byte_start, pref_size); 173 159 174 160 /* … … 179 165 180 166 /* Skip any whitespace before current token */ 181 ssize_t prev_token = current_token - 1;182 if ( (prev_token >= 0) && (tokens[prev_token].type == TOKTYPE_SPACE))167 int prev_token = current_token - 1; 168 if (prev_token != -1 && tokens[prev_token].type == TOKTYPE_SPACE) { 183 169 prev_token--; 184 170 } 171 185 172 /* 186 173 * It is a command if it is the first token or if it immediately 187 174 * follows a pipe token. 188 175 */ 189 if ( (prev_token < 0) || (tokens[prev_token].type == TOKTYPE_SPACE))176 if (prev_token == -1 || tokens[prev_token].type == TOKTYPE_SPACE) 190 177 cs->is_command = true; 191 178 else … … 262 249 if (cs != NULL) 263 250 free(cs); 264 if (tokens != NULL)265 free(tokens);266 251 267 252 return retval; -
uspace/app/bdsh/input.c
r8cc4ddb r1a5b252 67 67 int process_input(cliuser_t *usr) 68 68 { 69 token_t *tokens = calloc(WORD_MAX, sizeof(token_t));70 if (tokens == NULL)71 return ENOMEM;72 73 69 char *cmd[WORD_MAX]; 70 token_t tokens_space[WORD_MAX]; 71 token_t *tokens = tokens_space; 74 72 int rc = 0; 75 73 tokenizer_t tok; … … 79 77 char *redir_to = NULL; 80 78 81 if (usr->line == NULL) { 82 free(tokens); 79 if (NULL == usr->line) 83 80 return CL_EFAIL; 84 }85 81 86 82 rc = tok_init(&tok, usr->line, tokens, WORD_MAX); … … 213 209 } 214 210 tok_fini(&tok); 215 free(tokens);216 211 217 212 return rc; -
uspace/app/mkfat/fat.h
r8cc4ddb r1a5b252 38 38 #define BS_BLOCK 0 39 39 #define BS_SIZE 512 40 40 41 #define DIRENT_SIZE 32 41 42 #define FAT12_CLST_MAX 408543 #define FAT16_CLST_MAX 6552544 45 #define FAT12 1246 #define FAT16 1647 #define FAT32 3248 49 #define FAT_CLUSTER_DOUBLE_SIZE(a) ((a) / 4)50 42 51 43 typedef struct fat_bs { -
uspace/app/mkfat/mkfat.c
r8cc4ddb r1a5b252 35 35 * @brief Tool for creating new FAT file systems. 36 36 * 37 * Currently we can create 12/16/32-bit FAT.37 * Currently we can only create 16-bit FAT. 38 38 */ 39 39 … … 55 55 #define div_round_up(a, b) (((a) + (b) - 1) / (b)) 56 56 57 /** Defaultfile-system parameters */57 /** Predefined file-system parameters */ 58 58 enum { 59 default_sector_size = 512,60 default_sectors_per_cluster = 4,61 default_fat_count = 2,62 default_reserved_clusters = 2,63 default_media_descriptor = 0xF8 /**< fixed disk */59 sector_size = 512, 60 sectors_per_cluster = 8, 61 fat_count = 2, 62 reserved_clusters = 2, 63 media_descriptor = 0xF8 /**< fixed disk */ 64 64 }; 65 65 66 66 /** Configurable file-system parameters */ 67 67 typedef struct fat_cfg { 68 int fat_type; /* FAT12 = 12, FAT16 = 16, FAT32 = 32 */69 size_t sector_size;70 68 uint32_t total_sectors; 71 69 uint16_t root_ent_max; 72 uint32_t addt_res_sectors; 73 uint8_t sectors_per_cluster; 74 70 uint16_t addt_res_sectors; 71 } fat_cfg_t; 72 73 /** Derived file-system parameters */ 74 typedef struct fat_params { 75 struct fat_cfg cfg; 75 76 uint16_t reserved_sectors; 76 uint 32_t rootdir_sectors;77 uint16_t rootdir_sectors; 77 78 uint32_t fat_sectors; 78 uint32_t total_clusters; 79 uint8_t fat_count; 80 } fat_cfg_t; 79 uint16_t total_clusters; 80 } fat_params_t; 81 81 82 82 static void syntax_print(void); 83 83 84 static int fat_params_compute(struct fat_cfg *cfg); 85 static int fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id); 86 static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs); 84 static int fat_params_compute(struct fat_cfg const *cfg, 85 struct fat_params *par); 86 static int fat_blocks_write(struct fat_params const *par, 87 service_id_t service_id); 88 static void fat_bootsec_create(struct fat_params const *par, struct fat_bs *bs); 87 89 88 90 int main(int argc, char **argv) 89 91 { 92 struct fat_params par; 90 93 struct fat_cfg cfg; 91 94 … … 93 96 char *dev_path; 94 97 service_id_t service_id; 98 size_t block_size; 95 99 char *endptr; 96 100 aoff64_t dev_nblocks; 97 101 98 cfg.sector_size = default_sector_size;99 cfg.sectors_per_cluster = default_sectors_per_cluster;100 cfg.fat_count = default_fat_count;101 102 cfg.total_sectors = 0; 102 103 cfg.addt_res_sectors = 0; 103 104 cfg.root_ent_max = 128; 104 cfg.fat_type = FAT16;105 105 106 106 if (argc < 2) { … … 111 111 112 112 --argc; ++argv; 113 113 114 if (str_cmp(*argv, "--size") == 0) { 114 115 --argc; ++argv; … … 129 130 } 130 131 131 if (str_cmp(*argv, "--type") == 0) {132 --argc; ++argv;133 if (*argv == NULL) {134 printf(NAME ": Error, argument missing.\n");135 syntax_print();136 return 1;137 }138 139 cfg.fat_type = strtol(*argv, &endptr, 10);140 if (*endptr != '\0') {141 printf(NAME ": Error, invalid argument.\n");142 syntax_print();143 return 1;144 }145 146 --argc; ++argv;147 }148 149 132 if (argc != 1) { 150 133 printf(NAME ": Error, unexpected argument.\n"); … … 154 137 155 138 dev_path = *argv; 156 printf("Device: %s\n", dev_path);157 139 158 140 rc = loc_service_get_id(dev_path, &service_id, 0); … … 168 150 } 169 151 170 rc = block_get_bsize(service_id, & cfg.sector_size);152 rc = block_get_bsize(service_id, &block_size); 171 153 if (rc != EOK) { 172 154 printf(NAME ": Error determining device block size.\n"); … … 180 162 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n", 181 163 dev_nblocks); 182 if (!cfg.total_sectors || dev_nblocks < cfg.total_sectors) 183 cfg.total_sectors = dev_nblocks; 164 cfg.total_sectors = dev_nblocks; 165 } 166 167 if (block_size != 512) { 168 printf(NAME ": Error. Device block size is not 512 bytes.\n"); 169 return 2; 184 170 } 185 171 … … 189 175 } 190 176 191 if (cfg.fat_type != FAT12 && cfg.fat_type != FAT16 && cfg.fat_type != FAT32) { 192 printf(NAME ": Error. Unknown FAT type.\n"); 193 return 2; 194 } 195 196 printf(NAME ": Creating FAT%d filesystem on device %s.\n", cfg.fat_type, dev_path); 197 198 rc = fat_params_compute(&cfg); 177 printf(NAME ": Creating FAT filesystem on device %s.\n", dev_path); 178 179 rc = fat_params_compute(&cfg, &par); 199 180 if (rc != EOK) { 200 181 printf(NAME ": Invalid file-system parameters.\n"); … … 202 183 } 203 184 204 rc = fat_blocks_write(& cfg, service_id);185 rc = fat_blocks_write(&par, service_id); 205 186 if (rc != EOK) { 206 187 printf(NAME ": Error writing device.\n"); … … 216 197 static void syntax_print(void) 217 198 { 218 printf("syntax: mkfat [--size < sectors>] [--type 12|16|32] <device_name>\n");199 printf("syntax: mkfat [--size <num_blocks>] <device_name>\n"); 219 200 } 220 201 … … 224 205 * file system params. 225 206 */ 226 static int fat_params_compute(struct fat_cfg *cfg)207 static int fat_params_compute(struct fat_cfg const *cfg, struct fat_params *par) 227 208 { 228 209 uint32_t fat_bytes; … … 230 211 231 212 /* 232 * Make a conservative guess on the FAT size needed for the file 233 * system. The optimum could be potentially smaller since we 234 * do not subtract size of the FAT itself when computing the 235 * size of the data region. 236 */ 237 238 cfg->reserved_sectors = 1 + cfg->addt_res_sectors; 239 if (cfg->fat_type != FAT32) { 240 cfg->rootdir_sectors = div_round_up(cfg->root_ent_max * DIRENT_SIZE, 241 cfg->sector_size); 242 } else 243 cfg->rootdir_sectors = 0; 244 non_data_sectors_lb = cfg->reserved_sectors + cfg->rootdir_sectors; 245 246 cfg->total_clusters = div_round_up(cfg->total_sectors - non_data_sectors_lb, 247 cfg->sectors_per_cluster); 248 249 if ((cfg->fat_type == FAT12 && cfg->total_clusters > FAT12_CLST_MAX) || 250 (cfg->fat_type == FAT16 && (cfg->total_clusters <= FAT12_CLST_MAX || 251 cfg->total_clusters > FAT16_CLST_MAX)) || 252 (cfg->fat_type == FAT32 && cfg->total_clusters <= FAT16_CLST_MAX)) 253 return ENOSPC; 254 255 fat_bytes = div_round_up((cfg->total_clusters + 2) * 256 FAT_CLUSTER_DOUBLE_SIZE(cfg->fat_type), 2); 257 cfg->fat_sectors = div_round_up(fat_bytes, cfg->sector_size); 213 * Make a conservative guess on the FAT size needed for the file 214 * system. The optimum could be potentially smaller since we 215 * do not subtract size of the FAT itself when computing the 216 * size of the data region. 217 */ 218 219 par->reserved_sectors = 1 + cfg->addt_res_sectors; 220 par->rootdir_sectors = div_round_up(cfg->root_ent_max * DIRENT_SIZE, 221 sector_size); 222 non_data_sectors_lb = par->reserved_sectors + par->rootdir_sectors; 223 224 par->total_clusters = div_round_up(cfg->total_sectors - non_data_sectors_lb, 225 sectors_per_cluster); 226 227 fat_bytes = (par->total_clusters + 2) * 2; 228 par->fat_sectors = div_round_up(fat_bytes, sector_size); 229 230 par->cfg = *cfg; 258 231 259 232 return EOK; … … 261 234 262 235 /** Create file system with the given parameters. */ 263 static int fat_blocks_write(struct fat_ cfg const *cfg, service_id_t service_id)236 static int fat_blocks_write(struct fat_params const *par, service_id_t service_id) 264 237 { 265 238 aoff64_t addr; … … 270 243 struct fat_bs bs; 271 244 272 fat_bootsec_create( cfg, &bs);245 fat_bootsec_create(par, &bs); 273 246 274 247 rc = block_write_direct(service_id, BS_BLOCK, 1, &bs); … … 278 251 addr = BS_BLOCK + 1; 279 252 280 buffer = calloc( cfg->sector_size, 1);253 buffer = calloc(sector_size, 1); 281 254 if (buffer == NULL) 282 255 return ENOMEM; 283 memset(buffer, 0, cfg->sector_size);284 256 285 257 /* Reserved sectors */ 286 for (i = 0; i < cfg->reserved_sectors - 1; ++i) {258 for (i = 0; i < par->reserved_sectors - 1; ++i) { 287 259 rc = block_write_direct(service_id, addr, 1, buffer); 288 260 if (rc != EOK) … … 293 265 294 266 /* File allocation tables */ 295 for (i = 0; i < cfg->fat_count; ++i) {267 for (i = 0; i < fat_count; ++i) { 296 268 printf("Writing allocation table %d.\n", i + 1); 297 269 298 for (j = 0; j < cfg->fat_sectors; ++j) {299 memset(buffer, 0, cfg->sector_size);270 for (j = 0; j < par->fat_sectors; ++j) { 271 memset(buffer, 0, sector_size); 300 272 if (j == 0) { 301 buffer[0] = default_media_descriptor;273 buffer[0] = media_descriptor; 302 274 buffer[1] = 0xFF; 303 275 buffer[2] = 0xFF; 304 if (cfg->fat_type == FAT16) { 305 buffer[3] = 0xFF; 306 } else if (cfg->fat_type == FAT32) { 307 buffer[3] = 0x0F; 308 buffer[4] = 0xFF; 309 buffer[5] = 0xFF; 310 buffer[6] = 0xFF; 311 buffer[7] = 0x0F; 312 buffer[8] = 0xF8; 313 buffer[9] = 0xFF; 314 buffer[10] = 0xFF; 315 buffer[11] = 0x0F; 316 } 276 buffer[3] = 0xFF; 317 277 } 318 278 … … 325 285 } 326 286 287 printf("Writing root directory.\n"); 288 289 memset(buffer, 0, sector_size); 290 327 291 /* Root directory */ 328 printf("Writing root directory.\n"); 329 memset(buffer, 0, cfg->sector_size); 330 if (cfg->fat_type != FAT32) { 331 size_t idx; 332 for (idx = 0; idx < cfg->rootdir_sectors; ++idx) { 333 rc = block_write_direct(service_id, addr, 1, buffer); 334 if (rc != EOK) 335 return EIO; 336 337 ++addr; 338 } 339 } else { 340 for (i = 0; i < cfg->sectors_per_cluster; i++) { 341 rc = block_write_direct(service_id, addr, 1, buffer); 342 if (rc != EOK) 343 return EIO; 344 345 ++addr; 346 } 292 for (i = 0; i < par->rootdir_sectors; ++i) { 293 rc = block_write_direct(service_id, addr, 1, buffer); 294 if (rc != EOK) 295 return EIO; 296 297 ++addr; 347 298 } 348 299 … … 353 304 354 305 /** Construct boot sector with the given parameters. */ 355 static void fat_bootsec_create(struct fat_ cfg const *cfg, struct fat_bs *bs)306 static void fat_bootsec_create(struct fat_params const *par, struct fat_bs *bs) 356 307 { 357 308 memset(bs, 0, sizeof(*bs)); … … 364 315 365 316 /* BIOS Parameter Block */ 366 bs->bps = host2uint16_t_le(cfg->sector_size); 367 bs->spc = cfg->sectors_per_cluster; 368 bs->rscnt = host2uint16_t_le(cfg->reserved_sectors); 369 bs->fatcnt = cfg->fat_count; 370 bs->root_ent_max = host2uint16_t_le(cfg->root_ent_max); 371 372 if (cfg->total_sectors < 0x10000) { 373 bs->totsec16 = host2uint16_t_le(cfg->total_sectors); 374 bs->totsec32 = 0; 375 } else { 376 bs->totsec16 = 0; 377 bs->totsec32 = host2uint32_t_le(cfg->total_sectors); 378 } 379 380 bs->mdesc = default_media_descriptor; 317 bs->bps = host2uint16_t_le(sector_size); 318 bs->spc = sectors_per_cluster; 319 bs->rscnt = host2uint16_t_le(par->reserved_sectors); 320 bs->fatcnt = fat_count; 321 bs->root_ent_max = host2uint16_t_le(par->cfg.root_ent_max); 322 323 if (par->cfg.total_sectors < 0x10000) 324 bs->totsec16 = host2uint16_t_le(par->cfg.total_sectors); 325 else 326 bs->totsec16 = host2uint16_t_le(0); 327 328 bs->mdesc = media_descriptor; 329 bs->sec_per_fat = host2uint16_t_le(par->fat_sectors); 381 330 bs->sec_per_track = host2uint16_t_le(63); 382 bs->signature = host2uint16_t_be(0x55AA);383 331 bs->headcnt = host2uint16_t_le(6); 384 332 bs->hidden_sec = host2uint32_t_le(0); 385 333 386 if (cfg->fat_type == FAT32) { 387 bs->sec_per_fat = 0; 388 bs->fat32.sectors_per_fat = host2uint32_t_le(cfg->fat_sectors); 389 390 bs->fat32.pdn = 0x80; 391 bs->fat32.ebs = 0x29; 392 bs->fat32.id = host2uint32_t_be(0x12345678); 393 bs->fat32.root_cluster = 2; 394 395 memcpy(bs->fat32.label, "HELENOS_NEW", 11); 396 memcpy(bs->fat32.type, "FAT32 ", 8); 397 } else { 398 bs->sec_per_fat = host2uint16_t_le(cfg->fat_sectors); 399 bs->pdn = 0x80; 400 bs->ebs = 0x29; 401 bs->id = host2uint32_t_be(0x12345678); 402 403 memcpy(bs->label, "HELENOS_NEW", 11); 404 memcpy(bs->type, "FAT ", 8); 405 } 334 if (par->cfg.total_sectors >= 0x10000) 335 bs->totsec32 = host2uint32_t_le(par->cfg.total_sectors); 336 else 337 bs->totsec32 = host2uint32_t_le(0); 338 339 /* Extended BPB */ 340 bs->pdn = 0x80; 341 bs->ebs = 0x29; 342 bs->id = host2uint32_t_be(0x12345678); 343 344 memcpy(bs->label, "HELENOS_NEW", 11); 345 memcpy(bs->type, "FAT16 ", 8); 346 bs->signature = host2uint16_t_be(0x55AA); 406 347 } 407 348 -
uspace/app/sysinfo/sysinfo.c
r8cc4ddb r1a5b252 51 51 int rc; 52 52 char *ipath; 53 sysinfo_item_ val_type_t tag;53 sysinfo_item_tag_t tag; 54 54 55 55 if (argc != 2) { … … 60 60 ipath = argv[1]; 61 61 62 tag = sysinfo_get_ val_type(ipath);62 tag = sysinfo_get_tag(ipath); 63 63 64 64 /* Silence warning */ … … 75 75 case SYSINFO_VAL_DATA: 76 76 rc = print_item_data(ipath); 77 break;78 default:79 printf("Error: Sysinfo item '%s' with unknown value type.\n",80 ipath);81 rc = 2;82 77 break; 83 78 } -
uspace/app/tester/print/print2.c
r8cc4ddb r1a5b252 45 45 TPRINTF("Real output: [%d] [%3.2d] [%-3.2d] [%2.3d] [%-2.3d]\n\n", -1, -2, -3, -4, -5); 46 46 47 TPRINTF("Testing printf(\"%%lld %%3.2lld %%-3.2lld %%2.3lld %%-2.3lld\", (long long) -1, (long long) -2, (long long) -3, (long long) -4, (long long) -5):\n");48 TPRINTF("Expected output: [-1] [-02] [-03] [-004] [-005]\n");49 TPRINTF("Real output: [%lld] [%3.2lld] [%-3.2lld] [%2.3lld] [%-2.3lld]\n\n", (long long) -1, (long long) -2, (long long) -3, (long long) -4, (long long) -5);50 51 47 TPRINTF("Testing printf(\"%%#x %%5.3#x %%-5.3#x %%3.5#x %%-3.5#x\", 17, 18, 19, 20, 21):\n"); 52 48 TPRINTF("Expected output: [0x11] [0x012] [0x013] [0x00014] [0x00015]\n"); -
uspace/app/tester/stdio/stdio1.c
r8cc4ddb r1a5b252 39 39 { 40 40 FILE *file; 41 const char *file_name = "/ textdemo";41 const char *file_name = "/readme"; 42 42 43 43 TPRINTF("Open file \"%s\"...", file_name); -
uspace/app/trace/syscalls.c
r8cc4ddb r1a5b252 75 75 [SYS_UNREGISTER_IRQ] = { "unregister_irq", 2, V_ERRNO }, 76 76 77 [SYS_SYSINFO_GET_ VAL_TYPE] = { "sysinfo_get_val_type", 2, V_INTEGER },77 [SYS_SYSINFO_GET_TAG] = { "sysinfo_get_tag", 2, V_INTEGER }, 78 78 [SYS_SYSINFO_GET_VALUE] = { "sysinfo_get_value", 3, V_ERRNO }, 79 79 [SYS_SYSINFO_GET_DATA_SIZE] = { "sysinfo_get_data_size", 3, V_ERRNO }, -
uspace/dist/src/sysel/demos/htxtfile.sy
r8cc4ddb r1a5b252 35 35 var out_file : TextFile; 36 36 37 name = "/ textdemo";37 name = "/readme"; 38 38 39 39 in_file = new TextFile(); -
uspace/lib/c/arch/ia32/include/ddi.h
r8cc4ddb r1a5b252 43 43 if (port < (ioport8_t *) IO_SPACE_BOUNDARY) { 44 44 uint8_t val; 45 46 45 asm volatile ( 47 46 "inb %w[port], %b[val]\n" … … 49 48 : [port] "d" (port) 50 49 ); 51 52 50 return val; 53 } else 51 } else { 54 52 return (uint8_t) *port; 53 } 55 54 } 56 55 … … 59 58 if (port < (ioport16_t *) IO_SPACE_BOUNDARY) { 60 59 uint16_t val; 61 62 60 asm volatile ( 63 61 "inw %w[port], %w[val]\n" … … 65 63 : [port] "d" (port) 66 64 ); 67 68 65 return val; 69 } else 66 } else { 70 67 return (uint16_t) *port; 68 } 71 69 } 72 70 … … 75 73 if (port < (ioport32_t *) IO_SPACE_BOUNDARY) { 76 74 uint32_t val; 77 78 75 asm volatile ( 79 76 "inl %w[port], %[val]\n" … … 81 78 : [port] "d" (port) 82 79 ); 83 84 80 return val; 85 } else 81 } else { 86 82 return (uint32_t) *port; 83 } 87 84 } 88 85 … … 94 91 :: [val] "a" (val), [port] "d" (port) 95 92 ); 96 } else 93 } else { 97 94 *port = val; 95 } 98 96 } 99 97 … … 105 103 :: [val] "a" (val), [port] "d" (port) 106 104 ); 107 } else 105 } else { 108 106 *port = val; 107 } 109 108 } 110 109 … … 116 115 :: [val] "a" (val), [port] "d" (port) 117 116 ); 118 } else 117 } else { 119 118 *port = val; 119 } 120 120 } 121 121 -
uspace/lib/c/generic/async.c
r8cc4ddb r1a5b252 1777 1777 int async_hangup(async_sess_t *sess) 1778 1778 { 1779 async_exch_t *exch;1780 1781 1779 assert(sess); 1782 1780 1783 1781 if (atomic_get(&sess->refcnt) > 0) 1784 1782 return EBUSY; 1785 1786 fibril_mutex_lock(&async_sess_mutex);1787 1783 1788 1784 int rc = async_hangup_internal(sess->phone); 1789 1785 if (rc == EOK) 1790 1786 free(sess); 1791 1792 while (!list_empty(&sess->exch_list)) {1793 exch = (async_exch_t *)1794 list_get_instance(list_first(&sess->exch_list),1795 async_exch_t, sess_link);1796 1797 list_remove(&exch->sess_link);1798 list_remove(&exch->global_link);1799 async_hangup_internal(exch->phone);1800 free(exch);1801 }1802 1803 fibril_mutex_unlock(&async_sess_mutex);1804 1787 1805 1788 return rc; -
uspace/lib/c/generic/io/printf_core.c
r8cc4ddb r1a5b252 73 73 */ 74 74 #define PRINT_NUMBER_BUFFER_SIZE (64 + 5) 75 76 /** Get signed or unsigned integer argument */77 #define PRINTF_GET_INT_ARGUMENT(type, ap, flags) \78 ({ \79 unsigned type res; \80 \81 if ((flags) & __PRINTF_FLAG_SIGNED) { \82 signed type arg = va_arg((ap), signed type); \83 \84 if (arg < 0) { \85 res = -arg; \86 (flags) |= __PRINTF_FLAG_NEGATIVE; \87 } else \88 res = arg; \89 } else \90 res = va_arg((ap), unsigned type); \91 \92 res; \93 })94 75 95 76 /** Enumeration of possible arguments types. … … 850 831 size_t size; 851 832 uint64_t number; 852 853 833 switch (qualifier) { 854 834 case PrintfQualifierByte: 855 835 size = sizeof(unsigned char); 856 number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);836 number = (uint64_t) va_arg(ap, unsigned int); 857 837 break; 858 838 case PrintfQualifierShort: 859 839 size = sizeof(unsigned short); 860 number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);840 number = (uint64_t) va_arg(ap, unsigned int); 861 841 break; 862 842 case PrintfQualifierInt: 863 843 size = sizeof(unsigned int); 864 number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);844 number = (uint64_t) va_arg(ap, unsigned int); 865 845 break; 866 846 case PrintfQualifierLong: 867 847 size = sizeof(unsigned long); 868 number = PRINTF_GET_INT_ARGUMENT(long, ap, flags);848 number = (uint64_t) va_arg(ap, unsigned long); 869 849 break; 870 850 case PrintfQualifierLongLong: 871 851 size = sizeof(unsigned long long); 872 number = PRINTF_GET_INT_ARGUMENT(long long, ap, flags);852 number = (uint64_t) va_arg(ap, unsigned long long); 873 853 break; 874 854 case PrintfQualifierPointer: … … 885 865 counter = -counter; 886 866 goto out; 867 } 868 869 if (flags & __PRINTF_FLAG_SIGNED) { 870 if (number & (0x1 << (size * 8 - 1))) { 871 flags |= __PRINTF_FLAG_NEGATIVE; 872 873 if (size == sizeof(uint64_t)) { 874 number = -((int64_t) number); 875 } else { 876 number = ~number; 877 number &= 878 ~(0xFFFFFFFFFFFFFFFFll << 879 (size * 8)); 880 number++; 881 } 882 } 887 883 } 888 884 -
uspace/lib/c/generic/str.c
r8cc4ddb r1a5b252 2 2 * Copyright (c) 2005 Martin Decky 3 3 * Copyright (c) 2008 Jiri Svoboda 4 * Copyright (c) 2011 Martin Sucha5 * Copyright (c) 2011 Oleg Romanenko6 4 * All rights reserved. 7 5 * … … 641 639 } 642 640 643 /** Convert UTF16 string to string.644 *645 * Convert utf16 string @a src to string. The output is written to the buffer646 * specified by @a dest and @a size. @a size must be non-zero and the string647 * written will always be well-formed. Surrogate pairs also supported.648 *649 * @param dest Destination buffer.650 * @param size Size of the destination buffer.651 * @param src Source utf16 string.652 *653 * @return EOK, if success, negative otherwise.654 */655 int utf16_to_str(char *dest, size_t size, const uint16_t *src)656 {657 size_t idx = 0, dest_off = 0;658 wchar_t ch;659 int rc = EOK;660 661 /* There must be space for a null terminator in the buffer. */662 assert(size > 0);663 664 while (src[idx]) {665 if ((src[idx] & 0xfc00) == 0xd800) {666 if (src[idx + 1] && (src[idx + 1] & 0xfc00) == 0xdc00) {667 ch = 0x10000;668 ch += (src[idx] & 0x03FF) << 10;669 ch += (src[idx + 1] & 0x03FF);670 idx += 2;671 }672 else673 break;674 } else {675 ch = src[idx];676 idx++;677 }678 rc = chr_encode(ch, dest, &dest_off, size - 1);679 if (rc != EOK)680 break;681 }682 dest[dest_off] = '\0';683 return rc;684 }685 686 int str_to_utf16(uint16_t *dest, size_t size, const char *src)687 {688 int rc = EOK;689 size_t offset = 0;690 size_t idx = 0;691 wchar_t c;692 693 assert(size > 0);694 695 while ((c = str_decode(src, &offset, STR_NO_LIMIT)) != 0) {696 if (c > 0x10000) {697 if (idx + 2 >= size - 1) {698 rc = EOVERFLOW;699 break;700 }701 c = (c - 0x10000);702 dest[idx] = 0xD800 | (c >> 10);703 dest[idx + 1] = 0xDC00 | (c & 0x3FF);704 idx++;705 } else {706 dest[idx] = c;707 }708 709 idx++;710 if (idx >= size - 1) {711 rc = EOVERFLOW;712 break;713 }714 }715 716 dest[idx] = '\0';717 return rc;718 }719 720 721 641 /** Convert wide string to new string. 722 642 * … … 798 718 799 719 dest[dlen - 1] = '\0'; 800 }801 802 /** Convert string to wide string.803 *804 * Convert string @a src to wide string. A new wide NULL-terminated805 * string will be allocated on the heap.806 *807 * @param src Source string.808 */809 wchar_t *str_to_awstr(const char *str)810 {811 size_t len = str_length(str);812 813 wchar_t *wstr = calloc(len+1, sizeof(wchar_t));814 if (wstr == NULL)815 return NULL;816 817 str_to_wstr(wstr, len + 1, str);818 return wstr;819 720 } 820 721 … … 1115 1016 return dest; 1116 1017 } 1018 1117 1019 1118 1020 /** Convert initial part of string to unsigned long according to given base. -
uspace/lib/c/generic/sysinfo.c
r8cc4ddb r1a5b252 47 47 * 48 48 */ 49 sysinfo_item_ val_type_t sysinfo_get_val_type(const char *path)49 sysinfo_item_tag_t sysinfo_get_tag(const char *path) 50 50 { 51 return (sysinfo_item_ val_type_t) __SYSCALL2(SYS_SYSINFO_GET_VAL_TYPE,51 return (sysinfo_item_tag_t) __SYSCALL2(SYS_SYSINFO_GET_TAG, 52 52 (sysarg_t) path, (sysarg_t) str_size(path)); 53 53 } -
uspace/lib/c/include/bool.h
r8cc4ddb r1a5b252 37 37 38 38 #include <libarch/types.h> 39 #include <abi/bool.h>40 39 41 40 #define false 0 42 41 #define true 1 42 43 typedef uint8_t bool; 43 44 44 45 #endif -
uspace/lib/c/include/str.h
r8cc4ddb r1a5b252 1 1 /* 2 2 * Copyright (c) 2005 Martin Decky 3 * Copyright (c) 2011 Oleg Romanenko4 3 * All rights reserved. 5 4 * … … 84 83 extern char *wstr_to_astr(const wchar_t *src); 85 84 extern void str_to_wstr(wchar_t *dest, size_t dlen, const char *src); 86 extern wchar_t *str_to_awstr(const char *src);87 extern int utf16_to_str(char *dest, size_t size, const uint16_t *src);88 extern int str_to_utf16(uint16_t *dest, size_t size, const char *src);89 85 90 86 extern char *str_chr(const char *str, wchar_t ch); -
uspace/lib/c/include/sysinfo.h
r8cc4ddb r1a5b252 36 36 #define LIBC_SYSINFO_H_ 37 37 38 #include <sys/types.h> 39 #include <bool.h> 40 #include <abi/sysinfo.h> 38 #include <libc.h> 41 39 42 extern sysinfo_item_val_type_t sysinfo_get_val_type(const char *); 40 /** Sysinfo value types 41 * 42 */ 43 typedef enum { 44 SYSINFO_VAL_UNDEFINED = 0, 45 SYSINFO_VAL_VAL = 1, 46 SYSINFO_VAL_DATA = 2 47 } sysinfo_item_tag_t; 48 49 extern sysinfo_item_tag_t sysinfo_get_tag(const char *); 43 50 extern int sysinfo_get_value(const char *, sysarg_t *); 44 51 extern void *sysinfo_get_data(const char *, size_t *); -
uspace/lib/c/include/task.h
r8cc4ddb r1a5b252 37 37 38 38 #include <sys/types.h> 39 #include <abi/proc/task.h> 39 40 typedef uint64_t task_id_t; 40 41 41 42 typedef enum { -
uspace/lib/c/include/thread.h
r8cc4ddb r1a5b252 38 38 #include <libarch/thread.h> 39 39 #include <sys/types.h> 40 #include <abi/proc/thread.h> 40 41 typedef uint64_t thread_id_t; 41 42 42 43 extern int thread_create(void (*)(void *), void *, const char *, thread_id_t *); -
uspace/srv/fs/fat/Makefile
r8cc4ddb r1a5b252 39 39 fat_idx.c \ 40 40 fat_dentry.c \ 41 fat_directory.c \42 41 fat_fat.c 43 42 -
uspace/srv/fs/fat/fat.c
r8cc4ddb r1a5b252 2 2 * Copyright (c) 2006 Martin Decky 3 3 * Copyright (c) 2008 Jakub Jermar 4 * Copyright (c) 2011 Oleg Romanenko5 4 * All rights reserved. 6 5 * -
uspace/srv/fs/fat/fat.h
r8cc4ddb r1a5b252 1 1 /* 2 2 * Copyright (c) 2008 Jakub Jermar 3 * Copyright (c) 2011 Oleg Romanenko4 3 * All rights reserved. 5 4 * … … 47 46 #endif 48 47 49 #define min(a, b) ((a) < (b) ? (a) : (b))48 #define min(a, b) ((a) < (b) ? (a) : (b)) 50 49 51 50 /* … … 56 55 #define RSCNT(bs) uint16_t_le2host((bs)->rscnt) 57 56 #define FATCNT(bs) (bs)->fatcnt 58 59 #define SF(bs) (uint16_t_le2host((bs)->sec_per_fat) ? \ 60 uint16_t_le2host((bs)->sec_per_fat) : \ 61 uint32_t_le2host(bs->fat32.sectors_per_fat)) 62 57 #define SF(bs) uint16_t_le2host((bs)->sec_per_fat) 63 58 #define RDE(bs) uint16_t_le2host((bs)->root_ent_max) 64 65 #define TS(bs) (uint16_t_le2host((bs)->totsec16) ? \ 66 uint16_t_le2host((bs)->totsec16) : \ 67 uint32_t_le2host(bs->totsec32)) 68 69 #define BS_BLOCK 0 70 #define BS_SIZE 512 59 #define TS(bs) (uint16_t_le2host((bs)->totsec16) != 0 ? \ 60 uint16_t_le2host((bs)->totsec16) : \ 61 uint32_t_le2host(bs->totsec32)) 62 63 #define BS_BLOCK 0 64 #define BS_SIZE 512 71 65 72 66 typedef struct fat_bs { -
uspace/srv/fs/fat/fat_dentry.c
r8cc4ddb r1a5b252 1 1 /* 2 2 * Copyright (c) 2008 Jakub Jermar 3 * Copyright (c) 2011 Oleg Romanenko4 3 * All rights reserved. 5 4 * … … 40 39 #include <ctype.h> 41 40 #include <str.h> 42 #include <errno.h> 43 #include <byteorder.h> 44 #include <assert.h> 41 42 static bool is_d_char(const char ch) 43 { 44 if (isalnum(ch) || ch == '_') 45 return true; 46 else 47 return false; 48 } 45 49 46 50 /** Compare path component with the name read from the dentry. … … 76 80 } 77 81 78 void fat_dentry_name_get(const fat_dentry_t *d, char *buf) 79 { 80 unsigned int i; 81 82 for (i = 0; i < FAT_NAME_LEN; i++) { 83 if (d->name[i] == FAT_PAD) 84 break; 85 86 if (d->name[i] == FAT_DENTRY_E5_ESC) 87 *buf++ = 0xe5; 88 else { 89 if (d->lcase & FAT_LCASE_LOWER_NAME) 90 *buf++ = tolower(d->name[i]); 91 else 92 *buf++ = d->name[i]; 93 } 94 } 95 96 if (d->ext[0] != FAT_PAD) 97 *buf++ = '.'; 98 99 for (i = 0; i < FAT_EXT_LEN; i++) { 100 if (d->ext[i] == FAT_PAD) { 101 *buf = '\0'; 102 return; 103 } 104 105 if (d->ext[i] == FAT_DENTRY_E5_ESC) 106 *buf++ = 0xe5; 107 else { 108 if (d->lcase & FAT_LCASE_LOWER_EXT) 109 *buf++ = tolower(d->ext[i]); 110 else 111 *buf++ = d->ext[i]; 112 } 113 } 114 115 *buf = '\0'; 116 } 117 118 void fat_dentry_name_set(fat_dentry_t *d, const char *name) 119 { 120 unsigned int i; 121 const char fake_ext[] = " "; 122 bool lower_name = true; 123 bool lower_ext = true; 124 125 for (i = 0; i < FAT_NAME_LEN; i++) { 126 switch ((uint8_t) *name) { 127 case 0xe5: 128 d->name[i] = FAT_DENTRY_E5_ESC; 129 name++; 130 break; 131 case '\0': 132 case '.': 133 d->name[i] = FAT_PAD; 134 break; 135 default: 136 if (isalpha(*name)) { 137 if (!islower(*name)) 138 lower_name = false; 139 } 140 141 d->name[i] = toupper(*name++); 142 break; 143 } 144 } 145 146 if (*name++ != '.') 147 name = fake_ext; 148 149 for (i = 0; i < FAT_EXT_LEN; i++) { 150 switch ((uint8_t) *name) { 151 case 0xe5: 152 d->ext[i] = FAT_DENTRY_E5_ESC; 153 name++; 154 break; 155 case '\0': 156 d->ext[i] = FAT_PAD; 157 break; 158 default: 159 if (isalpha(*name)) { 160 if (!islower(*name)) 161 lower_ext = false; 162 } 163 164 d->ext[i] = toupper(*name++); 165 break; 166 } 167 } 168 169 if (lower_name) 170 d->lcase |= FAT_LCASE_LOWER_NAME; 171 else 172 d->lcase &= ~FAT_LCASE_LOWER_NAME; 173 174 if (lower_ext) 175 d->lcase |= FAT_LCASE_LOWER_EXT; 176 else 177 d->lcase &= ~FAT_LCASE_LOWER_EXT; 178 } 179 180 fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d) 181 { 182 if (d->attr == FAT_ATTR_LFN) { 183 /* long name entry */ 184 if (FAT_LFN_ORDER(d) & FAT_LFN_ERASED) 185 return FAT_DENTRY_FREE; 186 else 187 return FAT_DENTRY_LFN; 188 } 189 if (d->attr & FAT_ATTR_VOLLABEL) { 190 /* volume label entry */ 191 return FAT_DENTRY_SKIP; 192 } 193 if (d->name[0] == FAT_DENTRY_ERASED) { 194 /* not-currently-used entry */ 195 return FAT_DENTRY_FREE; 196 } 197 if (d->name[0] == FAT_DENTRY_UNUSED) { 198 /* never used entry */ 199 return FAT_DENTRY_LAST; 200 } 201 if (d->name[0] == FAT_DENTRY_DOT) { 202 /* 203 * Most likely '.' or '..'. 204 * It cannot occur in a regular file name. 205 */ 206 return FAT_DENTRY_SKIP; 207 } 208 return FAT_DENTRY_VALID; 209 } 210 211 /** Compute checksum of Node name. 212 * 213 * Returns an unsigned byte checksum computed on an unsigned byte 214 * array. The array must be 11 bytes long and is assumed to contain 215 * a name stored in the format of a MS-DOS directory entry. 216 * 217 * @param name Node name read from the dentry. 218 * 219 * @return An 8-bit unsigned checksum of the name. 220 */ 221 uint8_t fat_dentry_chksum(uint8_t *name) 222 { 223 uint8_t i, sum = 0; 224 225 for (i = 0; i < (FAT_NAME_LEN + FAT_EXT_LEN); i++) 226 sum = ((sum & 1) ? 0x80 : 0) + (sum >> 1) + name[i]; 227 228 return sum; 229 } 230 231 /** Get number of bytes in a string with size limit. 232 * 233 * @param str NULL-terminated (or not) string. 234 * @param size Maximum number of bytes to consider. 235 * 236 * @return Number of bytes in string (without 0 and ff). 237 * 238 */ 239 size_t fat_lfn_str_nlength(const uint16_t *str, size_t size) 240 { 241 size_t offset = 0; 242 243 while (offset < size) { 244 if (str[offset] == 0 || str[offset] == FAT_LFN_PAD) 245 break; 246 offset++; 247 } 248 return offset; 249 } 250 251 /** Get number of bytes in a FAT long entry occuped by characters. 252 * 253 * @param d FAT long entry. 254 * 255 * @return Number of bytes. 256 * 257 */ 258 size_t fat_lfn_size(const fat_dentry_t *d) 259 { 260 size_t size = 0; 261 262 size += fat_lfn_str_nlength(FAT_LFN_PART1(d), FAT_LFN_PART1_SIZE); 263 size += fat_lfn_str_nlength(FAT_LFN_PART2(d), FAT_LFN_PART2_SIZE); 264 size += fat_lfn_str_nlength(FAT_LFN_PART3(d), FAT_LFN_PART3_SIZE); 265 266 return size; 267 } 268 269 size_t fat_lfn_get_entry(const fat_dentry_t *d, uint16_t *dst, size_t *offset) 270 { 271 int i; 272 for (i = FAT_LFN_PART3_SIZE - 1; i >= 0 && *offset > 0; i--) { 273 if (d->lfn.part3[i] == 0 || d->lfn.part3[i] == FAT_LFN_PAD) 274 continue; 275 (*offset)--; 276 dst[(*offset)] = uint16_t_le2host(d->lfn.part3[i]); 277 } 278 for (i = FAT_LFN_PART2_SIZE - 1; i >= 0 && *offset > 0; i--) { 279 if (d->lfn.part2[i] == 0 || d->lfn.part2[i] == FAT_LFN_PAD) 280 continue; 281 (*offset)--; 282 dst[(*offset)] = uint16_t_le2host(d->lfn.part2[i]); 283 } 284 for (i = FAT_LFN_PART1_SIZE - 1; i >= 0 && *offset > 0; i--) { 285 if (d->lfn.part1[i] == 0 || d->lfn.part1[i] == FAT_LFN_PAD) 286 continue; 287 (*offset)--; 288 dst[(*offset)] = uint16_t_le2host(d->lfn.part1[i]); 289 } 290 return *offset; 291 } 292 293 size_t fat_lfn_set_entry(const uint16_t *src, size_t *offset, size_t size, 294 fat_dentry_t *d) 295 { 296 size_t idx; 297 for (idx = 0; idx < FAT_LFN_PART1_SIZE; idx++) { 298 if (*offset < size) { 299 d->lfn.part1[idx] = host2uint16_t_le(src[*offset]); 300 (*offset)++; 301 } else 302 d->lfn.part1[idx] = FAT_LFN_PAD; 303 } 304 for (idx = 0; idx < FAT_LFN_PART2_SIZE; idx++) { 305 if (*offset < size) { 306 d->lfn.part2[idx] = host2uint16_t_le(src[*offset]); 307 (*offset)++; 308 } else 309 d->lfn.part2[idx] = FAT_LFN_PAD; 310 } 311 for (idx = 0; idx < FAT_LFN_PART3_SIZE; idx++) { 312 if (*offset < size) { 313 d->lfn.part3[idx] = host2uint16_t_le(src[*offset]); 314 (*offset)++; 315 } else 316 d->lfn.part3[idx] = FAT_LFN_PAD; 317 } 318 319 if (src[*offset] == 0) 320 offset++; 321 FAT_LFN_ATTR(d) = FAT_ATTR_LFN; 322 d->lfn.type = 0; 323 d->lfn.firstc_lo = 0; 324 325 return *offset; 326 } 327 328 void str_to_ascii(char *dst, const char *src, size_t count, uint8_t pad) 329 { 330 wchar_t ch; 331 size_t off = 0; 332 size_t i = 0; 333 334 while (i < count) { 335 if ((ch = str_decode(src, &off, STR_NO_LIMIT)) != 0) { 336 if (ascii_check(ch) & IS_D_CHAR(ch)) 337 *dst = toupper(ch); 338 else 339 *dst = pad; 340 } else 341 break; 342 343 dst++; 344 i++; 345 } 346 *dst = '\0'; 347 } 348 349 bool fat_valid_name(const char *name) 350 { 351 wchar_t ch; 352 size_t offset=0; 353 bool result = true; 354 355 while ((ch = str_decode(name, &offset, STR_NO_LIMIT)) != 0) { 356 if (str_chr(FAT_STOP_CHARS, ch) != NULL) { 357 result = false; 358 break; 359 } 360 } 361 return result; 362 } 363 364 bool fat_valid_short_name(const char *name) 82 bool fat_dentry_name_verify(const char *name) 365 83 { 366 84 unsigned int i; 367 85 unsigned int dot = 0; 368 86 bool dot_found = false; 87 369 88 370 89 for (i = 0; name[i]; i++) { … … 377 96 } 378 97 } else { 379 if (! IS_D_CHAR(name[i]))98 if (!is_d_char(name[i])) 380 99 return false; 381 100 } … … 395 114 } 396 115 397 size_t utf16_length(const uint16_t *wstr) 398 { 399 size_t len = 0; 400 401 while (*wstr++ != 0) 402 len++; 403 404 return len; 116 void fat_dentry_name_get(const fat_dentry_t *d, char *buf) 117 { 118 unsigned int i; 119 120 for (i = 0; i < FAT_NAME_LEN; i++) { 121 if (d->name[i] == FAT_PAD) 122 break; 123 124 if (d->name[i] == FAT_DENTRY_E5_ESC) 125 *buf++ = 0xe5; 126 else { 127 if (d->lcase & FAT_LCASE_LOWER_NAME) 128 *buf++ = tolower(d->name[i]); 129 else 130 *buf++ = d->name[i]; 131 } 132 } 133 134 if (d->ext[0] != FAT_PAD) 135 *buf++ = '.'; 136 137 for (i = 0; i < FAT_EXT_LEN; i++) { 138 if (d->ext[i] == FAT_PAD) { 139 *buf = '\0'; 140 return; 141 } 142 143 if (d->ext[i] == FAT_DENTRY_E5_ESC) 144 *buf++ = 0xe5; 145 else { 146 if (d->lcase & FAT_LCASE_LOWER_EXT) 147 *buf++ = tolower(d->ext[i]); 148 else 149 *buf++ = d->ext[i]; 150 } 151 } 152 153 *buf = '\0'; 154 } 155 156 void fat_dentry_name_set(fat_dentry_t *d, const char *name) 157 { 158 unsigned int i; 159 const char fake_ext[] = " "; 160 bool lower_name = true; 161 bool lower_ext = true; 162 163 for (i = 0; i < FAT_NAME_LEN; i++) { 164 switch ((uint8_t) *name) { 165 case 0xe5: 166 d->name[i] = FAT_DENTRY_E5_ESC; 167 name++; 168 break; 169 case '\0': 170 case '.': 171 d->name[i] = FAT_PAD; 172 break; 173 default: 174 if (isalpha(*name)) { 175 if (!islower(*name)) 176 lower_name = false; 177 } 178 179 d->name[i] = toupper(*name++); 180 break; 181 } 182 } 183 184 if (*name++ != '.') 185 name = fake_ext; 186 187 for (i = 0; i < FAT_EXT_LEN; i++) { 188 switch ((uint8_t) *name) { 189 case 0xe5: 190 d->ext[i] = FAT_DENTRY_E5_ESC; 191 name++; 192 break; 193 case '\0': 194 d->ext[i] = FAT_PAD; 195 break; 196 default: 197 if (isalpha(*name)) { 198 if (!islower(*name)) 199 lower_ext = false; 200 } 201 202 d->ext[i] = toupper(*name++); 203 break; 204 } 205 } 206 207 if (lower_name) 208 d->lcase |= FAT_LCASE_LOWER_NAME; 209 else 210 d->lcase &= ~FAT_LCASE_LOWER_NAME; 211 212 if (lower_ext) 213 d->lcase |= FAT_LCASE_LOWER_EXT; 214 else 215 d->lcase &= ~FAT_LCASE_LOWER_EXT; 216 } 217 218 fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d) 219 { 220 if (d->attr & FAT_ATTR_VOLLABEL) { 221 /* volume label entry */ 222 return FAT_DENTRY_SKIP; 223 } 224 if (d->name[0] == FAT_DENTRY_ERASED) { 225 /* not-currently-used entry */ 226 return FAT_DENTRY_FREE; 227 } 228 if (d->name[0] == FAT_DENTRY_UNUSED) { 229 /* never used entry */ 230 return FAT_DENTRY_LAST; 231 } 232 if (d->name[0] == FAT_DENTRY_DOT) { 233 /* 234 * Most likely '.' or '..'. 235 * It cannot occur in a regular file name. 236 */ 237 return FAT_DENTRY_SKIP; 238 } 239 return FAT_DENTRY_VALID; 405 240 } 406 241 -
uspace/srv/fs/fat/fat_dentry.h
r8cc4ddb r1a5b252 1 1 /* 2 2 * Copyright (c) 2008 Jakub Jermar 3 * Copyright (c) 2011 Oleg Romanenko4 3 * All rights reserved. 5 4 * … … 38 37 #include <bool.h> 39 38 40 #define IS_D_CHAR(ch) (isalnum(ch) || ch == '_')41 #define FAT_STOP_CHARS "*?/\\\n\t|'"42 43 39 #define FAT_NAME_LEN 8 44 40 #define FAT_EXT_LEN 3 … … 48 44 #define FAT_EXT_PAD " " 49 45 50 #define FAT_ATTR_RDONLY 0x01 51 #define FAT_ATTR_HIDDEN 0x02 52 #define FAT_ATTR_SYSTEM 0x04 53 #define FAT_ATTR_VOLLABEL 0x08 54 #define FAT_ATTR_SUBDIR 0x10 55 #define FAT_ATTR_ARCHIVE 0x20 56 #define FAT_ATTR_LFN \ 57 (FAT_ATTR_RDONLY | FAT_ATTR_HIDDEN | FAT_ATTR_SYSTEM | FAT_ATTR_VOLLABEL) 58 46 #define FAT_ATTR_RDONLY (1 << 0) 47 #define FAT_ATTR_VOLLABEL (1 << 3) 48 #define FAT_ATTR_SUBDIR (1 << 4) 49 59 50 #define FAT_LCASE_LOWER_NAME 0x08 60 51 #define FAT_LCASE_LOWER_EXT 0x10 61 52 62 #define FAT_PAD ' ' 63 #define FAT_LFN_PAD 0xffff 64 #define FAT_SFN_CHAR '_' 53 #define FAT_PAD ' ' 65 54 66 55 #define FAT_DENTRY_UNUSED 0x00 … … 68 57 #define FAT_DENTRY_DOT 0x2e 69 58 #define FAT_DENTRY_ERASED 0xe5 70 #define FAT_LFN_LAST 0x4071 #define FAT_LFN_ERASED 0x8072 73 #define FAT_LFN_ORDER(d) ((d)->lfn.order)74 #define FAT_IS_LFN(d) \75 ((FAT_LFN_ORDER((d)) & FAT_LFN_LAST) == FAT_LFN_LAST)76 #define FAT_LFN_COUNT(d) \77 (FAT_LFN_ORDER((d)) ^ FAT_LFN_LAST)78 #define FAT_LFN_PART1(d) ((d)->lfn.part1)79 #define FAT_LFN_PART2(d) ((d)->lfn.part2)80 #define FAT_LFN_PART3(d) ((d)->lfn.part3)81 #define FAT_LFN_ATTR(d) ((d)->lfn.attr)82 #define FAT_LFN_CHKSUM(d) ((d)->lfn.check_sum)83 84 #define FAT_LFN_NAME_SIZE 26085 #define FAT_LFN_MAX_COUNT 2086 #define FAT_LFN_PART1_SIZE 587 #define FAT_LFN_PART2_SIZE 688 #define FAT_LFN_PART3_SIZE 289 #define FAT_LFN_ENTRY_SIZE \90 (FAT_LFN_PART1_SIZE + FAT_LFN_PART2_SIZE + FAT_LFN_PART3_SIZE)91 59 92 60 typedef enum { … … 94 62 FAT_DENTRY_LAST, 95 63 FAT_DENTRY_FREE, 96 FAT_DENTRY_VALID, 97 FAT_DENTRY_LFN 64 FAT_DENTRY_VALID 98 65 } fat_dentry_clsf_t; 99 66 100 typedef union { 101 struct { 102 uint8_t name[8]; 103 uint8_t ext[3]; 104 uint8_t attr; 105 uint8_t lcase; 106 uint8_t ctime_fine; 107 uint16_t ctime; 108 uint16_t cdate; 109 uint16_t adate; 110 union { 111 uint16_t eaidx; /* FAT12/FAT16 */ 112 uint16_t firstc_hi; /* FAT32 */ 113 } __attribute__ ((packed)); 114 uint16_t mtime; 115 uint16_t mdate; 116 union { 117 uint16_t firstc; /* FAT12/FAT16 */ 118 uint16_t firstc_lo; /* FAT32 */ 119 } __attribute__ ((packed)); 120 uint32_t size; 67 typedef struct { 68 uint8_t name[8]; 69 uint8_t ext[3]; 70 uint8_t attr; 71 uint8_t lcase; 72 uint8_t ctime_fine; 73 uint16_t ctime; 74 uint16_t cdate; 75 uint16_t adate; 76 union { 77 uint16_t eaidx; /* FAT12/FAT16 */ 78 uint16_t firstc_hi; /* FAT32 */ 121 79 } __attribute__ ((packed)); 122 struct { 123 uint8_t order; 124 uint16_t part1[FAT_LFN_PART1_SIZE]; 125 uint8_t attr; 126 uint8_t type; 127 uint8_t check_sum; 128 uint16_t part2[FAT_LFN_PART2_SIZE]; 129 uint16_t firstc_lo; /* MUST be 0 */ 130 uint16_t part3[FAT_LFN_PART3_SIZE]; 131 } __attribute__ ((packed)) lfn; 80 uint16_t mtime; 81 uint16_t mdate; 82 union { 83 uint16_t firstc; /* FAT12/FAT16 */ 84 uint16_t firstc_lo; /* FAT32 */ 85 } __attribute__ ((packed)); 86 uint32_t size; 132 87 } __attribute__ ((packed)) fat_dentry_t; 133 88 134 135 89 extern int fat_dentry_namecmp(char *, const char *); 90 extern bool fat_dentry_name_verify(const char *); 136 91 extern void fat_dentry_name_get(const fat_dentry_t *, char *); 137 92 extern void fat_dentry_name_set(fat_dentry_t *, const char *); 138 93 extern fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *); 139 extern uint8_t fat_dentry_chksum(uint8_t *);140 141 extern size_t fat_lfn_str_nlength(const uint16_t *, size_t);142 extern size_t fat_lfn_size(const fat_dentry_t *);143 extern size_t fat_lfn_get_entry(const fat_dentry_t *, uint16_t *, size_t *);144 extern size_t fat_lfn_set_entry(const uint16_t *, size_t *, size_t,145 fat_dentry_t *);146 147 extern void str_to_ascii(char *, const char *, size_t, uint8_t);148 extern size_t utf16_length(const uint16_t *);149 150 extern bool fat_valid_name(const char *);151 extern bool fat_valid_short_name(const char *);152 94 153 95 #endif -
uspace/srv/fs/fat/fat_fat.c
r8cc4ddb r1a5b252 1 1 /* 2 2 * Copyright (c) 2008 Jakub Jermar 3 * Copyright (c) 2011 Oleg Romanenko4 3 * All rights reserved. 5 4 * … … 30 29 /** @addtogroup fs 31 30 * @{ 32 */ 31 */ 33 32 34 33 /** … … 51 50 #include <mem.h> 52 51 53 #define IS_ODD(number) (number & 0x1) 52 /* 53 * Convenience macros for computing some frequently used values from the 54 * primitive boot sector members. 55 */ 56 #define RDS(bs) ((sizeof(fat_dentry_t) * RDE((bs))) / BPS((bs))) + \ 57 (((sizeof(fat_dentry_t) * RDE((bs))) % BPS((bs))) != 0) 58 #define SSA(bs) (RSCNT((bs)) + FATCNT((bs)) * SF((bs)) + RDS(bs)) 59 60 #define CLBN2PBN(bs, cl, bn) \ 61 (SSA((bs)) + ((cl) - FAT_CLST_FIRST) * SPC((bs)) + (bn) % SPC((bs))) 54 62 55 63 /** … … 57 65 * during allocation of clusters. The lock does not have to be held durring 58 66 * deallocation of clusters. 59 */ 67 */ 60 68 static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock); 61 69 … … 69 77 * @param numc If non-NULL, output argument holding the number of 70 78 * clusters seen during the walk. 71 * @param max_clusters Maximum number of clusters to visit. 79 * @param max_clusters Maximum number of clusters to visit. 72 80 * 73 81 * @return EOK on success or a negative error code. 74 82 */ 75 int 83 int 76 84 fat_cluster_walk(fat_bs_t *bs, service_id_t service_id, fat_cluster_t firstc, 77 fat_cluster_t *lastc, uint 32_t *numc, uint32_t max_clusters)78 { 79 uint32_t clusters = 0;80 fat_cluster_t clst = firstc, clst_last1 = FAT_CLST_LAST1(bs);81 fat_cluster_t clst _bad = FAT_CLST_BAD(bs);85 fat_cluster_t *lastc, uint16_t *numc, uint16_t max_clusters) 86 { 87 block_t *b; 88 uint16_t clusters = 0; 89 fat_cluster_t clst = firstc; 82 90 int rc; 83 91 … … 91 99 } 92 100 93 while (clst < clst_last1 && clusters < max_clusters) { 101 while (clst < FAT_CLST_LAST1 && clusters < max_clusters) { 102 aoff64_t fsec; /* sector offset relative to FAT1 */ 103 unsigned fidx; /* FAT1 entry index */ 104 94 105 assert(clst >= FAT_CLST_FIRST); 95 106 if (lastc) 96 107 *lastc = clst; /* remember the last cluster number */ 97 108 fsec = (clst * sizeof(fat_cluster_t)) / BPS(bs); 109 fidx = clst % (BPS(bs) / sizeof(fat_cluster_t)); 98 110 /* read FAT1 */ 99 rc = fat_get_cluster(bs, service_id, FAT1, clst, &clst); 100 if (rc != EOK) 101 return rc; 102 103 assert(clst != clst_bad); 111 rc = block_get(&b, service_id, RSCNT(bs) + fsec, 112 BLOCK_FLAGS_NONE); 113 if (rc != EOK) 114 return rc; 115 clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); 116 assert(clst != FAT_CLST_BAD); 117 rc = block_put(b); 118 if (rc != EOK) 119 return rc; 104 120 clusters++; 105 121 } 106 122 107 if (lastc && clst < clst_last1)123 if (lastc && clst < FAT_CLST_LAST1) 108 124 *lastc = clst; 109 125 if (numc) … … 135 151 return ELIMIT; 136 152 137 if ( !FAT_IS_FAT32(bs) && nodep->firstc == FAT_CLST_ROOT)153 if (nodep->firstc == FAT_CLST_ROOT) 138 154 goto fall_through; 139 155 … … 162 178 if (rc != EOK) 163 179 return rc; 164 180 165 181 /* 166 182 * Update the "current" cluster cache. … … 182 198 * @param clp If not NULL, address where the cluster containing bn 183 199 * will be stored. 184 * stored 200 * stored 185 201 * @param bn Block number. 186 202 * @param flags Flags passed to libblock. … … 192 208 fat_cluster_t fcl, fat_cluster_t *clp, aoff64_t bn, int flags) 193 209 { 194 uint 32_t clusters;195 u int32_tmax_clusters;210 uint16_t clusters; 211 unsigned max_clusters; 196 212 fat_cluster_t c; 197 213 int rc; … … 203 219 return ELIMIT; 204 220 205 if ( !FAT_IS_FAT32(bs) &&fcl == FAT_CLST_ROOT) {221 if (fcl == FAT_CLST_ROOT) { 206 222 /* root directory special case */ 207 223 assert(bn < RDS(bs)); … … 237 253 * @return EOK on success or a negative error code. 238 254 */ 239 int 240 fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos) 255 int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos) 241 256 { 242 257 block_t *b; … … 260 275 return rc; 261 276 } 262 277 263 278 if (o >= pos) 264 279 return EOK; 265 280 266 281 /* zero out the initial part of the new cluster chain */ 267 282 for (o = boundary; o < pos; o += BPS(bs)) { … … 289 304 * @return EOK or a negative error code. 290 305 */ 291 static int292 fat_get_cluster_fat12(fat_bs_t *bs, service_id_t service_id, unsigned fatno,293 fat_cluster_t clst, fat_cluster_t *value)294 {295 block_t *b, *b1;296 uint16_t byte1, byte2;297 aoff64_t offset;298 int rc;299 300 offset = (clst + clst / 2);301 if (offset / BPS(bs) >= SF(bs))302 return ERANGE;303 304 rc = block_get(&b, service_id, RSCNT(bs) + SF(bs) * fatno +305 offset / BPS(bs), BLOCK_FLAGS_NONE);306 if (rc != EOK)307 return rc;308 309 byte1 = ((uint8_t *) b->data)[offset % BPS(bs)];310 /* This cluster access spans a sector boundary. Check only for FAT12 */311 if ((offset % BPS(bs)) + 1 == BPS(bs)) {312 /* Is this the last sector of FAT? */313 if (offset / BPS(bs) < SF(bs)) {314 /* No, read the next sector */315 rc = block_get(&b1, service_id, 1 + RSCNT(bs) +316 SF(bs) * fatno + offset / BPS(bs),317 BLOCK_FLAGS_NONE);318 if (rc != EOK) {319 block_put(b);320 return rc;321 }322 /*323 * Combining value with last byte of current sector and324 * first byte of next sector325 */326 byte2 = ((uint8_t*) b1->data)[0];327 328 rc = block_put(b1);329 if (rc != EOK) {330 block_put(b);331 return rc;332 }333 } else {334 /* Yes. This is the last sector of FAT */335 block_put(b);336 return ERANGE;337 }338 } else339 byte2 = ((uint8_t *) b->data)[(offset % BPS(bs)) + 1];340 341 *value = uint16_t_le2host(byte1 | (byte2 << 8));342 if (IS_ODD(clst))343 *value = (*value) >> 4;344 else345 *value = (*value) & FAT12_MASK;346 347 rc = block_put(b);348 349 return rc;350 }351 352 /** Get cluster from the first FAT.353 *354 * @param bs Buffer holding the boot sector for the file system.355 * @param service_id Service ID for the file system.356 * @param clst Cluster which to get.357 * @param value Output argument holding the value of the cluster.358 *359 * @return EOK or a negative error code.360 */361 static int362 fat_get_cluster_fat16(fat_bs_t *bs, service_id_t service_id, unsigned fatno,363 fat_cluster_t clst, fat_cluster_t *value)364 {365 block_t *b;366 aoff64_t offset;367 int rc;368 369 offset = (clst * FAT16_CLST_SIZE);370 371 rc = block_get(&b, service_id, RSCNT(bs) + SF(bs) * fatno +372 offset / BPS(bs), BLOCK_FLAGS_NONE);373 if (rc != EOK)374 return rc;375 376 *value = uint16_t_le2host(*(uint16_t *)(b->data + offset % BPS(bs)));377 378 rc = block_put(b);379 380 return rc;381 }382 383 /** Get cluster from the first FAT.384 *385 * @param bs Buffer holding the boot sector for the file system.386 * @param service_id Service ID for the file system.387 * @param clst Cluster which to get.388 * @param value Output argument holding the value of the cluster.389 *390 * @return EOK or a negative error code.391 */392 static int393 fat_get_cluster_fat32(fat_bs_t *bs, service_id_t service_id, unsigned fatno,394 fat_cluster_t clst, fat_cluster_t *value)395 {396 block_t *b;397 aoff64_t offset;398 int rc;399 400 offset = (clst * FAT32_CLST_SIZE);401 402 rc = block_get(&b, service_id, RSCNT(bs) + SF(bs) * fatno +403 offset / BPS(bs), BLOCK_FLAGS_NONE);404 if (rc != EOK)405 return rc;406 407 *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs))) &408 FAT32_MASK;409 410 rc = block_put(b);411 412 return rc;413 }414 415 416 /** Get cluster from the first FAT.417 *418 * @param bs Buffer holding the boot sector for the file system.419 * @param service_id Service ID for the file system.420 * @param clst Cluster which to get.421 * @param value Output argument holding the value of the cluster.422 *423 * @return EOK or a negative error code.424 */425 306 int 426 307 fat_get_cluster(fat_bs_t *bs, service_id_t service_id, unsigned fatno, 427 308 fat_cluster_t clst, fat_cluster_t *value) 428 309 { 429 int rc; 430 431 assert(fatno < FATCNT(bs)); 432 433 if (FAT_IS_FAT12(bs)) 434 rc = fat_get_cluster_fat12(bs, service_id, fatno, clst, value); 435 else if (FAT_IS_FAT16(bs)) 436 rc = fat_get_cluster_fat16(bs, service_id, fatno, clst, value); 437 else 438 rc = fat_get_cluster_fat32(bs, service_id, fatno, clst, value); 439 440 return rc; 441 } 442 443 /** Set cluster in one instance of FAT. 444 * 445 * @param bs Buffer holding the boot sector for the file system. 446 * @param service_id Service ID for the file system. 447 * @param fatno Number of the FAT instance where to make the change. 448 * @param clst Cluster which is to be set. 449 * @param value Value to set the cluster with. 450 * 451 * @return EOK on success or a negative error code. 452 */ 453 static int 454 fat_set_cluster_fat12(fat_bs_t *bs, service_id_t service_id, unsigned fatno, 455 fat_cluster_t clst, fat_cluster_t value) 456 { 457 block_t *b, *b1 = NULL; 458 aoff64_t offset; 459 uint16_t byte1, byte2; 460 int rc; 461 462 offset = (clst + clst / 2); 463 if (offset / BPS(bs) >= SF(bs)) 464 return ERANGE; 465 310 block_t *b; 311 fat_cluster_t *cp; 312 int rc; 313 466 314 rc = block_get(&b, service_id, RSCNT(bs) + SF(bs) * fatno + 467 offset/ BPS(bs), BLOCK_FLAGS_NONE);315 (clst * sizeof(fat_cluster_t)) / BPS(bs), BLOCK_FLAGS_NONE); 468 316 if (rc != EOK) 469 317 return rc; 470 471 byte1 = ((uint8_t*) b->data)[offset % BPS(bs)]; 472 bool border = false; 473 /* This cluster access spans a sector boundary. */ 474 if ((offset % BPS(bs)) + 1 == BPS(bs)) { 475 /* Is it the last sector of FAT? */ 476 if (offset / BPS(bs) < SF(bs)) { 477 /* No, read the next sector */ 478 rc = block_get(&b1, service_id, 1 + RSCNT(bs) + 479 SF(bs) * fatno + offset / BPS(bs), 480 BLOCK_FLAGS_NONE); 481 if (rc != EOK) { 482 block_put(b); 483 return rc; 484 } 485 /* 486 * Combining value with last byte of current sector and 487 * first byte of next sector 488 */ 489 byte2 = ((uint8_t *) b1->data)[0]; 490 border = true; 491 } else { 492 /* Yes. This is the last sector of FAT */ 493 block_put(b); 494 return ERANGE; 495 } 496 } else 497 byte2 = ((uint8_t*) b->data)[(offset % BPS(bs)) + 1]; 498 499 if (IS_ODD(clst)) { 500 byte1 &= 0x0f; 501 byte2 = 0; 502 value = (value << 4); 503 } else { 504 byte1 = 0; 505 byte2 &= 0xf0; 506 value &= FAT12_MASK; 507 } 508 509 byte1 = byte1 | (value & 0xff); 510 byte2 = byte2 | (value >> 8); 511 512 ((uint8_t *) b->data)[(offset % BPS(bs))] = byte1; 513 if (border) { 514 ((uint8_t *) b1->data)[0] = byte2; 515 516 b1->dirty = true; 517 rc = block_put(b1); 518 if (rc != EOK) { 519 block_put(b); 520 return rc; 521 } 522 } else 523 ((uint8_t *) b->data)[(offset % BPS(bs)) + 1] = byte2; 524 525 b->dirty = true; /* need to sync block */ 318 cp = (fat_cluster_t *)b->data + 319 clst % (BPS(bs) / sizeof(fat_cluster_t)); 320 *value = uint16_t_le2host(*cp); 526 321 rc = block_put(b); 527 528 return rc; 529 } 530 531 /** Set cluster in one instance of FAT. 532 * 533 * @param bs Buffer holding the boot sector for the file system. 534 * @param service_id Service ID for the file system. 535 * @param fatno Number of the FAT instance where to make the change. 536 * @param clst Cluster which is to be set. 537 * @param value Value to set the cluster with. 538 * 539 * @return EOK on success or a negative error code. 540 */ 541 static int 542 fat_set_cluster_fat16(fat_bs_t *bs, service_id_t service_id, unsigned fatno, 543 fat_cluster_t clst, fat_cluster_t value) 544 { 545 block_t *b; 546 aoff64_t offset; 547 int rc; 548 549 offset = (clst * FAT16_CLST_SIZE); 550 551 rc = block_get(&b, service_id, RSCNT(bs) + SF(bs) * fatno + 552 offset / BPS(bs), BLOCK_FLAGS_NONE); 553 if (rc != EOK) 554 return rc; 555 556 *(uint16_t *)(b->data + offset % BPS(bs)) = host2uint16_t_le(value); 557 558 b->dirty = true; /* need to sync block */ 559 rc = block_put(b); 560 561 return rc; 562 } 563 564 /** Set cluster in one instance of FAT. 565 * 566 * @param bs Buffer holding the boot sector for the file system. 567 * @param service_id Service ID for the file system. 568 * @param fatno Number of the FAT instance where to make the change. 569 * @param clst Cluster which is to be set. 570 * @param value Value to set the cluster with. 571 * 572 * @return EOK on success or a negative error code. 573 */ 574 static int 575 fat_set_cluster_fat32(fat_bs_t *bs, service_id_t service_id, unsigned fatno, 576 fat_cluster_t clst, fat_cluster_t value) 577 { 578 block_t *b; 579 aoff64_t offset; 580 int rc; 581 fat_cluster_t temp; 582 583 offset = (clst * FAT32_CLST_SIZE); 584 585 rc = block_get(&b, service_id, RSCNT(bs) + SF(bs) * fatno + 586 offset / BPS(bs), BLOCK_FLAGS_NONE); 587 if (rc != EOK) 588 return rc; 589 590 temp = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs))); 591 temp &= 0xf0000000; 592 temp |= (value & FAT32_MASK); 593 *(uint32_t *)(b->data + offset % BPS(bs)) = host2uint32_t_le(temp); 594 595 b->dirty = true; /* need to sync block */ 596 rc = block_put(b); 597 322 598 323 return rc; 599 324 } … … 613 338 fat_cluster_t clst, fat_cluster_t value) 614 339 { 340 block_t *b; 341 fat_cluster_t *cp; 615 342 int rc; 616 343 617 344 assert(fatno < FATCNT(bs)); 618 619 if (FAT_IS_FAT12(bs)) 620 rc = fat_set_cluster_fat12(bs, service_id, fatno, clst, value); 621 else if (FAT_IS_FAT16(bs)) 622 rc = fat_set_cluster_fat16(bs, service_id, fatno, clst, value); 623 else 624 rc = fat_set_cluster_fat32(bs, service_id, fatno, clst, value); 625 345 rc = block_get(&b, service_id, RSCNT(bs) + SF(bs) * fatno + 346 (clst * sizeof(fat_cluster_t)) / BPS(bs), BLOCK_FLAGS_NONE); 347 if (rc != EOK) 348 return rc; 349 cp = (fat_cluster_t *)b->data + 350 clst % (BPS(bs) / sizeof(fat_cluster_t)); 351 *cp = host2uint16_t_le(value); 352 b->dirty = true; /* need to sync block */ 353 rc = block_put(b); 626 354 return rc; 627 355 } … … 641 369 uint8_t fatno; 642 370 unsigned c; 643 fat_cluster_t clst_last1 = FAT_CLST_LAST1(bs); 644 int rc; 645 646 for (fatno = FAT1 + 1; fatno < FATCNT(bs); fatno++) { 371 int rc; 372 373 for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) { 647 374 for (c = 0; c < nclsts; c++) { 648 375 rc = fat_set_cluster(bs, service_id, fatno, lifo[c], 649 c == 0 ? clst_last1 : lifo[c - 1]);376 c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]); 650 377 if (rc != EOK) 651 378 return rc; … … 677 404 fat_cluster_t *mcl, fat_cluster_t *lcl) 678 405 { 679 fat_cluster_t *lifo; /* stack for storing free cluster numbers */ 680 unsigned found = 0; /* top of the free cluster number stack */ 681 fat_cluster_t clst, value, clst_last1 = FAT_CLST_LAST1(bs); 682 int rc = EOK; 406 block_t *blk; 407 fat_cluster_t *lifo; /* stack for storing free cluster numbers */ 408 unsigned found = 0; /* top of the free cluster number stack */ 409 unsigned b, c, cl; 410 int rc; 683 411 684 412 lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t)); … … 690 418 */ 691 419 fibril_mutex_lock(&fat_alloc_lock); 692 for (clst = FAT_CLST_FIRST; clst < CC(bs) + 2 && found < nclsts; 693 clst++) { 694 rc = fat_get_cluster(bs, service_id, FAT1, clst, &value); 695 if (rc != EOK) 696 break; 697 698 if (value == FAT_CLST_RES0) { 420 for (b = 0, cl = 0; b < SF(bs); b++) { 421 rc = block_get(&blk, service_id, RSCNT(bs) + b, 422 BLOCK_FLAGS_NONE); 423 if (rc != EOK) 424 goto error; 425 for (c = 0; c < BPS(bs) / sizeof(fat_cluster_t); c++, cl++) { 699 426 /* 700 * The cluster is free. Put it into our stack 701 * of found clusters and mark it as non-free. 427 * Check if the entire cluster is physically there. 428 * This check becomes necessary when the file system is 429 * created with fewer total sectors than how many is 430 * inferred from the size of the file allocation table 431 * or when the last cluster ends beyond the end of the 432 * device. 702 433 */ 703 lifo[found] = clst; 704 rc = fat_set_cluster(bs, service_id, FAT1, clst, 705 (found == 0) ? clst_last1 : lifo[found - 1]); 706 if (rc != EOK) 707 break; 708 709 found++; 434 if ((cl >= FAT_CLST_FIRST) && 435 CLBN2PBN(bs, cl, SPC(bs) - 1) >= TS(bs)) { 436 rc = block_put(blk); 437 if (rc != EOK) 438 goto error; 439 goto out; 440 } 441 442 fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; 443 if (uint16_t_le2host(*clst) == FAT_CLST_RES0) { 444 /* 445 * The cluster is free. Put it into our stack 446 * of found clusters and mark it as non-free. 447 */ 448 lifo[found] = cl; 449 *clst = (found == 0) ? 450 host2uint16_t_le(FAT_CLST_LAST1) : 451 host2uint16_t_le(lifo[found - 1]); 452 blk->dirty = true; /* need to sync block */ 453 if (++found == nclsts) { 454 /* we are almost done */ 455 rc = block_put(blk); 456 if (rc != EOK) 457 goto error; 458 /* update the shadow copies of FAT */ 459 rc = fat_alloc_shadow_clusters(bs, 460 service_id, lifo, nclsts); 461 if (rc != EOK) 462 goto error; 463 *mcl = lifo[found - 1]; 464 *lcl = lifo[0]; 465 free(lifo); 466 fibril_mutex_unlock(&fat_alloc_lock); 467 return EOK; 468 } 469 } 710 470 } 711 } 712 713 if (rc == EOK && found == nclsts) { 714 rc = fat_alloc_shadow_clusters(bs, service_id, lifo, nclsts); 715 if (rc == EOK) { 716 *mcl = lifo[found - 1]; 717 *lcl = lifo[0]; 471 rc = block_put(blk); 472 if (rc != EOK) { 473 error: 474 fibril_mutex_unlock(&fat_alloc_lock); 718 475 free(lifo); 719 fibril_mutex_unlock(&fat_alloc_lock); 720 return EOK; 476 return rc; 721 477 } 722 478 } 723 724 /* If something wrong - free the clusters */ 479 out: 480 fibril_mutex_unlock(&fat_alloc_lock); 481 482 /* 483 * We could not find enough clusters. Now we need to free the clusters 484 * we have allocated so far. 485 */ 725 486 while (found--) { 726 (void)fat_set_cluster(bs, service_id, FAT1, lifo[found],487 rc = fat_set_cluster(bs, service_id, FAT1, lifo[found], 727 488 FAT_CLST_RES0); 728 } 729 489 if (rc != EOK) { 490 free(lifo); 491 return rc; 492 } 493 } 494 730 495 free(lifo); 731 fibril_mutex_unlock(&fat_alloc_lock);732 733 496 return ENOSPC; 734 497 } … … 746 509 { 747 510 unsigned fatno; 748 fat_cluster_t nextc , clst_bad = FAT_CLST_BAD(bs);511 fat_cluster_t nextc; 749 512 int rc; 750 513 751 514 /* Mark all clusters in the chain as free in all copies of FAT. */ 752 while (firstc < FAT_CLST_LAST1(bs)) { 753 assert(firstc >= FAT_CLST_FIRST && firstc < clst_bad); 754 515 while (firstc < FAT_CLST_LAST1) { 516 assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD); 755 517 rc = fat_get_cluster(bs, service_id, FAT1, firstc, &nextc); 756 518 if (rc != EOK) 757 519 return rc; 758 759 for (fatno = FAT1; fatno < FATCNT(bs); fatno++) { 520 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) { 760 521 rc = fat_set_cluster(bs, service_id, fatno, firstc, 761 522 FAT_CLST_RES0); … … 779 540 * @return EOK on success or a negative error code. 780 541 */ 781 int fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, 542 int 543 fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, 782 544 fat_cluster_t lcl) 783 545 { … … 797 559 } else { 798 560 rc = fat_cluster_walk(bs, service_id, nodep->firstc, 799 &lastc, NULL, (uint 32_t) -1);561 &lastc, NULL, (uint16_t) -1); 800 562 if (rc != EOK) 801 563 return rc; 802 564 } 803 565 804 for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {805 rc = fat_set_cluster(bs, nodep->idx->service_id, 806 fatno,lastc, mcl);566 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) { 567 rc = fat_set_cluster(bs, nodep->idx->service_id, fatno, 568 lastc, mcl); 807 569 if (rc != EOK) 808 570 return rc; … … 828 590 int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lcl) 829 591 { 830 fat_cluster_t clst_last1 = FAT_CLST_LAST1(bs);831 592 int rc; 832 593 service_id_t service_id = nodep->idx->service_id; … … 855 616 856 617 /* Terminate the cluster chain in all copies of FAT. */ 857 for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {618 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) { 858 619 rc = fat_set_cluster(bs, service_id, fatno, lcl, 859 clst_last1);620 FAT_CLST_LAST1); 860 621 if (rc != EOK) 861 622 return rc; … … 912 673 913 674 /* Check number of FATs. */ 914 if ( FATCNT(bs)== 0)675 if (bs->fatcnt == 0) 915 676 return ENOTSUP; 916 677 917 678 /* Check total number of sectors. */ 918 if (TS(bs) == 0) 679 680 if (bs->totsec16 == 0 && bs->totsec32 == 0) 919 681 return ENOTSUP; 920 682 921 683 if (bs->totsec16 != 0 && bs->totsec32 != 0 && 922 bs->totsec16 != bs->totsec32) 684 bs->totsec16 != bs->totsec32) 923 685 return ENOTSUP; 924 686 … … 928 690 929 691 /* Check number of sectors per FAT. */ 930 if ( SF(bs)== 0)692 if (bs->sec_per_fat == 0) 931 693 return ENOTSUP; 932 694 … … 938 700 * sanitized to support file systems with this property. 939 701 */ 940 if ( !FAT_IS_FAT32(bs) &&941 (RDE(bs) * sizeof(fat_dentry_t)) % BPS(bs) != 0)702 if ((uint16_t_le2host(bs->root_ent_max) * sizeof(fat_dentry_t)) % 703 uint16_t_le2host(bs->bps) != 0) 942 704 return ENOTSUP; 943 705 944 706 /* Check signature of each FAT. */ 945 for (fat_no = 0; fat_no < FATCNT(bs); fat_no++) { 707 708 for (fat_no = 0; fat_no < bs->fatcnt; fat_no++) { 946 709 rc = fat_get_cluster(bs, service_id, fat_no, 0, &e0); 947 710 if (rc != EOK) … … 952 715 return EIO; 953 716 954 /* 955 * Check that first byte of FAT contains the media descriptor. 956 */ 717 /* Check that first byte of FAT contains the media descriptor. */ 957 718 if ((e0 & 0xff) != bs->mdesc) 958 719 return ENOTSUP; … … 962 723 * set to one. 963 724 */ 964 if (!FAT_IS_FAT12(bs) && 965 ((e0 >> 8) != (FAT_MASK(bs) >> 8) || e1 != FAT_MASK(bs))) 725 if ((e0 >> 8) != 0xff || e1 != 0xffff) 966 726 return ENOTSUP; 967 727 } … … 972 732 /** 973 733 * @} 974 */ 734 */ -
uspace/srv/fs/fat/fat_fat.h
r8cc4ddb r1a5b252 1 1 /* 2 2 * Copyright (c) 2008 Jakub Jermar 3 * Copyright (c) 2011 Oleg Romanenko4 3 * All rights reserved. 5 4 * … … 30 29 /** @addtogroup fs 31 30 * @{ 32 */ 31 */ 33 32 34 33 #ifndef FAT_FAT_FAT_H_ … … 41 40 #define FAT1 0 42 41 43 #define FAT_CLST_RES0 0 44 #define FAT_CLST_RES1 1 45 #define FAT_CLST_FIRST 2 46 47 #define FAT32_CLST_BAD 0x0ffffff7 48 #define FAT32_CLST_LAST1 0x0ffffff8 49 #define FAT32_CLST_LAST8 0x0fffffff 50 51 #define FAT12_MASK 0x0fff 52 #define FAT16_MASK 0xffff 53 #define FAT32_MASK 0x0fffffff 54 55 #define FAT12_CLST_MAX 4085 56 #define FAT16_CLST_MAX 65525 57 58 /* Size in bytes for cluster value of FAT */ 59 #define FAT12_CLST_SIZE 2 60 #define FAT16_CLST_SIZE 2 61 #define FAT32_CLST_SIZE 4 42 #define FAT_CLST_RES0 0x0000 43 #define FAT_CLST_RES1 0x0001 44 #define FAT_CLST_FIRST 0x0002 45 #define FAT_CLST_BAD 0xfff7 46 #define FAT_CLST_LAST1 0xfff8 47 #define FAT_CLST_LAST8 0xffff 62 48 63 49 /* internally used to mark root directory's parent */ … … 66 52 #define FAT_CLST_ROOT FAT_CLST_RES1 67 53 68 /*69 * Convenience macros for computing some frequently used values from the70 * primitive boot sector members.71 */72 #define RDS(bs) ((sizeof(fat_dentry_t) * RDE((bs))) / BPS((bs))) + \73 (((sizeof(fat_dentry_t) * RDE((bs))) % BPS((bs))) != 0)74 #define SSA(bs) (RSCNT((bs)) + FATCNT((bs)) * SF((bs)) + RDS(bs))75 #define DS(bs) (TS(bs) - SSA(bs))76 #define CC(bs) (DS(bs) / SPC(bs))77 78 #define CLBN2PBN(bs, cl, bn) \79 (SSA((bs)) + ((cl) - FAT_CLST_FIRST) * SPC((bs)) + (bn) % SPC((bs)))80 81 #define FAT_IS_FAT12(bs) (CC(bs) < FAT12_CLST_MAX)82 #define FAT_IS_FAT16(bs) \83 ((CC(bs) >= FAT12_CLST_MAX) && (CC(bs) < FAT16_CLST_MAX))84 #define FAT_IS_FAT32(bs) (CC(bs) >= FAT16_CLST_MAX)85 86 #define FAT_CLST_SIZE(bs) \87 (FAT_IS_FAT32(bs) ? FAT32_CLST_SIZE : FAT16_CLST_SIZE)88 89 #define FAT_MASK(bs) \90 (FAT_IS_FAT12(bs) ? FAT12_MASK : \91 (FAT_IS_FAT32(bs) ? FAT32_MASK : FAT16_MASK))92 93 #define FAT_CLST_LAST1(bs) (FAT32_CLST_LAST1 & FAT_MASK((bs)))94 #define FAT_CLST_LAST8(bs) (FAT32_CLST_LAST8 & FAT_MASK((bs)))95 #define FAT_CLST_BAD(bs) (FAT32_CLST_BAD & FAT_MASK((bs)))96 97 #define FAT_ROOT_CLST(bs) \98 (FAT_IS_FAT32(bs) ? uint32_t_le2host(bs->fat32.root_cluster) : \99 FAT_CLST_ROOT)100 101 54 /* forward declarations */ 102 55 struct block; … … 104 57 struct fat_bs; 105 58 106 typedef uint 32_t fat_cluster_t;59 typedef uint16_t fat_cluster_t; 107 60 108 #define fat_clusters_get(numc, bs, sid, fc) \109 fat_cluster_walk((bs), ( sid), (fc), NULL, (numc), (uint32_t) -1)61 #define fat_clusters_get(numc, bs, dh, fc) \ 62 fat_cluster_walk((bs), (dh), (fc), NULL, (numc), (uint16_t) -1) 110 63 extern int fat_cluster_walk(struct fat_bs *, service_id_t, fat_cluster_t, 111 fat_cluster_t *, uint 32_t *, uint32_t);64 fat_cluster_t *, uint16_t *, uint16_t); 112 65 113 66 extern int fat_block_get(block_t **, struct fat_bs *, struct fat_node *, -
uspace/srv/fs/fat/fat_idx.c
r8cc4ddb r1a5b252 116 116 #define UPH_BUCKETS (1 << UPH_BUCKETS_LOG) 117 117 118 #define UPH_ SID_KEY 0118 #define UPH_DH_KEY 0 119 119 #define UPH_PFC_KEY 1 120 120 #define UPH_PDI_KEY 2 … … 122 122 static hash_index_t pos_hash(unsigned long key[]) 123 123 { 124 service_id_t service_id = (service_id_t)key[UPH_ SID_KEY];124 service_id_t service_id = (service_id_t)key[UPH_DH_KEY]; 125 125 fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY]; 126 126 unsigned pdi = (unsigned)key[UPH_PDI_KEY]; … … 150 150 static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item) 151 151 { 152 service_id_t service_id = (service_id_t)key[UPH_ SID_KEY];152 service_id_t service_id = (service_id_t)key[UPH_DH_KEY]; 153 153 fat_cluster_t pfc; 154 154 unsigned pdi; … … 190 190 #define UIH_BUCKETS (1 << UIH_BUCKETS_LOG) 191 191 192 #define UIH_ SID_KEY 0192 #define UIH_DH_KEY 0 193 193 #define UIH_INDEX_KEY 1 194 194 195 195 static hash_index_t idx_hash(unsigned long key[]) 196 196 { 197 service_id_t service_id = (service_id_t)key[UIH_ SID_KEY];197 service_id_t service_id = (service_id_t)key[UIH_DH_KEY]; 198 198 fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY]; 199 199 … … 209 209 static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item) 210 210 { 211 service_id_t service_id = (service_id_t)key[UIH_ SID_KEY];211 service_id_t service_id = (service_id_t)key[UIH_DH_KEY]; 212 212 fs_index_t index; 213 213 fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link); … … 402 402 403 403 unsigned long ikey[] = { 404 [UIH_ SID_KEY] = service_id,404 [UIH_DH_KEY] = service_id, 405 405 [UIH_INDEX_KEY] = fidx->index, 406 406 }; … … 420 420 link_t *l; 421 421 unsigned long pkey[] = { 422 [UPH_ SID_KEY] = service_id,422 [UPH_DH_KEY] = service_id, 423 423 [UPH_PFC_KEY] = pfc, 424 424 [UPH_PDI_KEY] = pdi, … … 439 439 440 440 unsigned long ikey[] = { 441 [UIH_ SID_KEY] = service_id,441 [UIH_DH_KEY] = service_id, 442 442 [UIH_INDEX_KEY] = fidx->index, 443 443 }; … … 458 458 { 459 459 unsigned long pkey[] = { 460 [UPH_ SID_KEY] = idx->service_id,460 [UPH_DH_KEY] = idx->service_id, 461 461 [UPH_PFC_KEY] = idx->pfc, 462 462 [UPH_PDI_KEY] = idx->pdi, … … 471 471 { 472 472 unsigned long pkey[] = { 473 [UPH_ SID_KEY] = idx->service_id,473 [UPH_DH_KEY] = idx->service_id, 474 474 [UPH_PFC_KEY] = idx->pfc, 475 475 [UPH_PDI_KEY] = idx->pdi, … … 487 487 link_t *l; 488 488 unsigned long ikey[] = { 489 [UIH_ SID_KEY] = service_id,489 [UIH_DH_KEY] = service_id, 490 490 [UIH_INDEX_KEY] = index, 491 491 }; … … 509 509 { 510 510 unsigned long ikey[] = { 511 [UIH_ SID_KEY] = idx->service_id,511 [UIH_DH_KEY] = idx->service_id, 512 512 [UIH_INDEX_KEY] = idx->index, 513 513 }; … … 571 571 { 572 572 unsigned long ikey[] = { 573 [UIH_ SID_KEY] = service_id573 [UIH_DH_KEY] = service_id 574 574 }; 575 575 unsigned long pkey[] = { 576 [UPH_ SID_KEY] = service_id576 [UPH_DH_KEY] = service_id 577 577 }; 578 578 -
uspace/srv/fs/fat/fat_ops.c
r8cc4ddb r1a5b252 1 1 /* 2 2 * Copyright (c) 2008 Jakub Jermar 3 * Copyright (c) 2011 Oleg Romanenko4 3 * All rights reserved. 5 4 * … … 30 29 /** @addtogroup fs 31 30 * @{ 32 */ 31 */ 33 32 34 33 /** … … 40 39 #include "fat_dentry.h" 41 40 #include "fat_fat.h" 42 #include "fat_directory.h"43 41 #include "../../vfs/vfs.h" 44 42 #include <libfs.h> … … 58 56 #include <align.h> 59 57 #include <malloc.h> 60 #include <str.h>61 58 62 59 #define FAT_NODE(node) ((node) ? (fat_node_t *) (node)->data : NULL) … … 107 104 node->dirty = false; 108 105 node->lastc_cached_valid = false; 109 node->lastc_cached_value = 0;106 node->lastc_cached_value = FAT_CLST_LAST1; 110 107 node->currc_cached_valid = false; 111 108 node->currc_cached_bn = 0; 112 node->currc_cached_value = 0;109 node->currc_cached_value = FAT_CLST_LAST1; 113 110 } 114 111 … … 119 116 fat_dentry_t *d; 120 117 int rc; 121 118 122 119 assert(node->dirty); 123 120 124 121 bs = block_bb_get(node->idx->service_id); 125 122 126 123 /* Read the block that contains the dentry of interest. */ 127 124 rc = _fat_block_get(&b, bs, node->idx->service_id, node->idx->pfc, … … 139 136 d->attr = FAT_ATTR_SUBDIR; 140 137 } 141 138 142 139 /* TODO: update other fields? (e.g time fields) */ 143 140 144 141 b->dirty = true; /* need to sync block */ 145 142 rc = block_put(b); … … 258 255 fn->data = nodep; 259 256 nodep->bp = fn; 260 257 261 258 *nodepp = nodep; 262 259 return EOK; … … 294 291 * We must instantiate the node from the file system. 295 292 */ 296 293 297 294 assert(idxp->pfc); 298 295 … … 312 309 313 310 d = ((fat_dentry_t *)b->data) + (idxp->pdi % DPS(bs)); 314 if (FAT_IS_FAT32(bs)) {315 nodep->firstc = uint16_t_le2host(d->firstc_lo) |316 (uint16_t_le2host(d->firstc_hi) << 16);317 } else318 nodep->firstc = uint16_t_le2host(d->firstc);319 320 311 if (d->attr & FAT_ATTR_SUBDIR) { 321 /* 312 /* 322 313 * The only directory which does not have this bit set is the 323 314 * root directory itself. The root directory node is handled … … 325 316 */ 326 317 nodep->type = FAT_DIRECTORY; 327 328 318 /* 329 319 * Unfortunately, the 'size' field of the FAT dentry is not … … 331 321 * size of the directory by walking the FAT. 332 322 */ 333 uint 32_t clusters;323 uint16_t clusters; 334 324 rc = fat_clusters_get(&clusters, bs, idxp->service_id, 335 nodep->firstc);325 uint16_t_le2host(d->firstc)); 336 326 if (rc != EOK) { 337 327 (void) block_put(b); … … 344 334 nodep->size = uint32_t_le2host(d->size); 345 335 } 346 336 nodep->firstc = uint16_t_le2host(d->firstc); 347 337 nodep->lnkcnt = 1; 348 338 nodep->refcnt = 1; … … 373 363 int fat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component) 374 364 { 365 fat_bs_t *bs; 375 366 fat_node_t *parentp = FAT_NODE(pfn); 376 char name[FAT_LFN_NAME_SIZE]; 367 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; 368 unsigned i, j; 369 unsigned blocks; 377 370 fat_dentry_t *d; 378 371 service_id_t service_id; 372 block_t *b; 379 373 int rc; 380 374 … … 382 376 service_id = parentp->idx->service_id; 383 377 fibril_mutex_unlock(&parentp->idx->lock); 384 385 fat_directory_t di; 386 rc = fat_directory_open(parentp, &di); 387 if (rc != EOK) 388 return rc; 389 390 while (fat_directory_read(&di, name, &d) == EOK) { 391 if (fat_dentry_namecmp(name, component) == 0) { 392 /* hit */ 393 fat_node_t *nodep; 394 aoff64_t o = di.pos % 395 (BPS(di.bs) / sizeof(fat_dentry_t)); 396 fat_idx_t *idx = fat_idx_get_by_pos(service_id, 397 parentp->firstc, di.bnum * DPS(di.bs) + o); 398 if (!idx) { 399 /* 400 * Can happen if memory is low or if we 401 * run out of 32-bit indices. 402 */ 403 rc = fat_directory_close(&di); 404 return (rc == EOK) ? ENOMEM : rc; 378 379 bs = block_bb_get(service_id); 380 blocks = parentp->size / BPS(bs); 381 for (i = 0; i < blocks; i++) { 382 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE); 383 if (rc != EOK) 384 return rc; 385 for (j = 0; j < DPS(bs); j++) { 386 d = ((fat_dentry_t *)b->data) + j; 387 switch (fat_classify_dentry(d)) { 388 case FAT_DENTRY_SKIP: 389 case FAT_DENTRY_FREE: 390 continue; 391 case FAT_DENTRY_LAST: 392 /* miss */ 393 rc = block_put(b); 394 *rfn = NULL; 395 return rc; 396 default: 397 case FAT_DENTRY_VALID: 398 fat_dentry_name_get(d, name); 399 break; 405 400 } 406 rc = fat_node_get_core(&nodep, idx); 407 fibril_mutex_unlock(&idx->lock); 408 if (rc != EOK) { 409 (void) fat_directory_close(&di); 401 if (fat_dentry_namecmp(name, component) == 0) { 402 /* hit */ 403 fat_node_t *nodep; 404 fat_idx_t *idx = fat_idx_get_by_pos(service_id, 405 parentp->firstc, i * DPS(bs) + j); 406 if (!idx) { 407 /* 408 * Can happen if memory is low or if we 409 * run out of 32-bit indices. 410 */ 411 rc = block_put(b); 412 return (rc == EOK) ? ENOMEM : rc; 413 } 414 rc = fat_node_get_core(&nodep, idx); 415 fibril_mutex_unlock(&idx->lock); 416 if (rc != EOK) { 417 (void) block_put(b); 418 return rc; 419 } 420 *rfn = FS_NODE(nodep); 421 rc = block_put(b); 422 if (rc != EOK) 423 (void) fat_node_put(*rfn); 410 424 return rc; 411 425 } 412 *rfn = FS_NODE(nodep); 413 rc = fat_directory_close(&di); 414 if (rc != EOK) 415 (void) fat_node_put(*rfn); 416 return rc; 417 } else { 418 rc = fat_directory_next(&di); 419 if (rc != EOK) 420 break; 421 } 422 } 423 (void) fat_directory_close(&di); 426 } 427 rc = block_put(b); 428 if (rc != EOK) 429 return rc; 430 } 431 424 432 *rfn = NULL; 425 433 return EOK; … … 583 591 fat_bs_t *bs; 584 592 block_t *b; 585 fat_directory_t di; 586 fat_dentry_t de; 593 unsigned i, j; 594 unsigned blocks; 595 fat_cluster_t mcl, lcl; 587 596 int rc; 588 597 … … 598 607 fibril_mutex_unlock(&childp->lock); 599 608 600 if (!fat_valid_name(name)) 609 if (!fat_dentry_name_verify(name)) { 610 /* 611 * Attempt to create unsupported name. 612 */ 601 613 return ENOTSUP; 602 614 } 615 616 /* 617 * Get us an unused parent node's dentry or grow the parent and allocate 618 * a new one. 619 */ 620 603 621 fibril_mutex_lock(&parentp->idx->lock); 604 622 bs = block_bb_get(parentp->idx->service_id); 605 rc = fat_directory_open(parentp, &di); 623 624 blocks = parentp->size / BPS(bs); 625 626 for (i = 0; i < blocks; i++) { 627 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE); 628 if (rc != EOK) { 629 fibril_mutex_unlock(&parentp->idx->lock); 630 return rc; 631 } 632 for (j = 0; j < DPS(bs); j++) { 633 d = ((fat_dentry_t *)b->data) + j; 634 switch (fat_classify_dentry(d)) { 635 case FAT_DENTRY_SKIP: 636 case FAT_DENTRY_VALID: 637 /* skipping used and meta entries */ 638 continue; 639 case FAT_DENTRY_FREE: 640 case FAT_DENTRY_LAST: 641 /* found an empty slot */ 642 goto hit; 643 } 644 } 645 rc = block_put(b); 646 if (rc != EOK) { 647 fibril_mutex_unlock(&parentp->idx->lock); 648 return rc; 649 } 650 } 651 j = 0; 652 653 /* 654 * We need to grow the parent in order to create a new unused dentry. 655 */ 656 if (parentp->firstc == FAT_CLST_ROOT) { 657 /* Can't grow the root directory. */ 658 fibril_mutex_unlock(&parentp->idx->lock); 659 return ENOSPC; 660 } 661 rc = fat_alloc_clusters(bs, parentp->idx->service_id, 1, &mcl, &lcl); 606 662 if (rc != EOK) { 607 663 fibril_mutex_unlock(&parentp->idx->lock); 608 664 return rc; 609 665 } 610 666 rc = fat_zero_cluster(bs, parentp->idx->service_id, mcl); 667 if (rc != EOK) { 668 (void) fat_free_clusters(bs, parentp->idx->service_id, mcl); 669 fibril_mutex_unlock(&parentp->idx->lock); 670 return rc; 671 } 672 rc = fat_append_clusters(bs, parentp, mcl, lcl); 673 if (rc != EOK) { 674 (void) fat_free_clusters(bs, parentp->idx->service_id, mcl); 675 fibril_mutex_unlock(&parentp->idx->lock); 676 return rc; 677 } 678 parentp->size += BPS(bs) * SPC(bs); 679 parentp->dirty = true; /* need to sync node */ 680 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE); 681 if (rc != EOK) { 682 fibril_mutex_unlock(&parentp->idx->lock); 683 return rc; 684 } 685 d = (fat_dentry_t *)b->data; 686 687 hit: 611 688 /* 612 689 * At this point we only establish the link between the parent and the … … 615 692 * dentry data is kept in the child node structure. 616 693 */ 617 memset(&de, 0, sizeof(fat_dentry_t)); 618 619 rc = fat_directory_write(&di, name, &de); 620 if (rc != EOK) { 621 (void) fat_directory_close(&di); 622 fibril_mutex_unlock(&parentp->idx->lock); 623 return rc; 624 } 625 rc = fat_directory_close(&di); 626 if (rc != EOK) { 627 fibril_mutex_unlock(&parentp->idx->lock); 628 return rc; 629 } 630 694 memset(d, 0, sizeof(fat_dentry_t)); 695 fat_dentry_name_set(d, name); 696 b->dirty = true; /* need to sync block */ 697 rc = block_put(b); 631 698 fibril_mutex_unlock(&parentp->idx->lock); 699 if (rc != EOK) 700 return rc; 632 701 633 702 fibril_mutex_lock(&childp->idx->lock); 634 703 635 704 if (childp->type == FAT_DIRECTORY) { 636 705 /* … … 651 720 d = (fat_dentry_t *) b->data; 652 721 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) || 653 ( bcmp(d->name, FAT_NAME_DOT, FAT_NAME_LEN)) == 0) {722 (str_cmp((char *) d->name, FAT_NAME_DOT)) == 0) { 654 723 memset(d, 0, sizeof(fat_dentry_t)); 655 724 memcpy(d->name, FAT_NAME_DOT, FAT_NAME_LEN); … … 661 730 d++; 662 731 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) || 663 ( bcmp(d->name, FAT_NAME_DOT_DOT, FAT_NAME_LEN) == 0)) {732 (str_cmp((char *) d->name, FAT_NAME_DOT_DOT) == 0)) { 664 733 memset(d, 0, sizeof(fat_dentry_t)); 665 734 memcpy(d->name, FAT_NAME_DOT_DOT, FAT_NAME_LEN); 666 735 memcpy(d->ext, FAT_EXT_PAD, FAT_EXT_LEN); 667 736 d->attr = FAT_ATTR_SUBDIR; 668 d->firstc = (parentp->firstc == FAT_ ROOT_CLST(bs)) ?669 host2uint16_t_le(FAT_CLST_R OOTPAR) :737 d->firstc = (parentp->firstc == FAT_CLST_ROOT) ? 738 host2uint16_t_le(FAT_CLST_RES0) : 670 739 host2uint16_t_le(parentp->firstc); 671 740 /* TODO: initialize also the date/time members. */ … … 681 750 682 751 childp->idx->pfc = parentp->firstc; 683 childp->idx->pdi = di.pos; /* di.pos holds absolute position of SFN entry */752 childp->idx->pdi = i * DPS(bs) + j; 684 753 fibril_mutex_unlock(&childp->idx->lock); 685 754 … … 701 770 fat_node_t *parentp = FAT_NODE(pfn); 702 771 fat_node_t *childp = FAT_NODE(cfn); 772 fat_bs_t *bs; 773 fat_dentry_t *d; 774 block_t *b; 703 775 bool has_children; 704 776 int rc; … … 706 778 if (!parentp) 707 779 return EBUSY; 708 780 709 781 rc = fat_has_children(&has_children, cfn); 710 782 if (rc != EOK) … … 717 789 assert(childp->lnkcnt == 1); 718 790 fibril_mutex_lock(&childp->idx->lock); 719 720 fat_directory_t di; 721 rc = fat_directory_open(parentp, &di); 722 if (rc != EOK) 791 bs = block_bb_get(childp->idx->service_id); 792 793 rc = _fat_block_get(&b, bs, childp->idx->service_id, childp->idx->pfc, 794 NULL, (childp->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs), 795 BLOCK_FLAGS_NONE); 796 if (rc != EOK) 723 797 goto error; 724 rc = fat_directory_seek(&di, childp->idx->pdi); 725 if (rc != EOK) 726 goto error; 727 rc = fat_directory_erase(&di); 728 if (rc != EOK) 729 goto error; 730 rc = fat_directory_close(&di); 798 d = (fat_dentry_t *)b->data + 799 (childp->idx->pdi % (BPS(bs) / sizeof(fat_dentry_t))); 800 /* mark the dentry as not-currently-used */ 801 d->name[0] = FAT_DENTRY_ERASED; 802 b->dirty = true; /* need to sync block */ 803 rc = block_put(b); 731 804 if (rc != EOK) 732 805 goto error; … … 747 820 748 821 error: 749 (void) fat_directory_close(&di); 822 fibril_mutex_unlock(&parentp->idx->lock); 823 fibril_mutex_unlock(&childp->lock); 750 824 fibril_mutex_unlock(&childp->idx->lock); 751 fibril_mutex_unlock(&childp->lock);752 fibril_mutex_unlock(&parentp->lock);753 825 return rc; 754 826 } … … 767 839 return EOK; 768 840 } 769 841 770 842 fibril_mutex_lock(&nodep->idx->lock); 771 843 bs = block_bb_get(nodep->idx->service_id); … … 775 847 for (i = 0; i < blocks; i++) { 776 848 fat_dentry_t *d; 777 849 778 850 rc = fat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NONE); 779 851 if (rc != EOK) { … … 803 875 if (rc != EOK) { 804 876 fibril_mutex_unlock(&nodep->idx->lock); 805 return rc; 877 return rc; 806 878 } 807 879 } … … 874 946 fat_bs_t *bs; 875 947 int rc; 876 948 877 949 /* Check for option enabling write through. */ 878 950 if (str_cmp(opts, "wtcache") == 0) … … 931 1003 return ENOMEM; 932 1004 } 933 934 1005 fs_node_initialize(rfn); 935 1006 fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); … … 956 1027 957 1028 rootp->type = FAT_DIRECTORY; 958 rootp->firstc = FAT_ ROOT_CLST(bs);1029 rootp->firstc = FAT_CLST_ROOT; 959 1030 rootp->refcnt = 1; 960 1031 rootp->lnkcnt = 0; /* FS root is not linked */ 961 962 if (FAT_IS_FAT32(bs)) { 963 uint32_t clusters; 964 rc = fat_clusters_get(&clusters, bs, service_id, rootp->firstc); 965 if (rc != EOK) { 966 free(rfn); 967 free(rootp); 968 (void) block_cache_fini(service_id); 969 block_fini(service_id); 970 fat_idx_fini_by_service_id(service_id); 971 return ENOTSUP; 972 } 973 rootp->size = BPS(bs) * SPC(bs) * clusters; 974 } else 975 rootp->size = RDE(bs) * sizeof(fat_dentry_t); 976 1032 rootp->size = RDE(bs) * sizeof(fat_dentry_t); 977 1033 rootp->idx = ridxp; 978 1034 ridxp->nodep = rootp; 979 1035 rootp->bp = rfn; 980 1036 rfn->data = rootp; 981 1037 982 1038 fibril_mutex_unlock(&ridxp->lock); 983 1039 … … 1008 1064 return EBUSY; 1009 1065 } 1010 1066 1011 1067 /* 1012 1068 * Put the root node and force it to the FAT free node list. … … 1085 1141 } 1086 1142 } else { 1143 unsigned bnum; 1087 1144 aoff64_t spos = pos; 1088 char name[FAT_ LFN_NAME_SIZE];1145 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; 1089 1146 fat_dentry_t *d; 1090 1147 … … 1093 1150 assert(BPS(bs) % sizeof(fat_dentry_t) == 0); 1094 1151 1095 fat_directory_t di; 1096 rc = fat_directory_open(nodep, &di); 1097 if (rc != EOK) 1098 goto err; 1099 rc = fat_directory_seek(&di, pos); 1100 if (rc != EOK) { 1101 (void) fat_directory_close(&di); 1102 goto err; 1103 } 1104 1105 rc = fat_directory_read(&di, name, &d); 1106 if (rc == EOK) 1107 goto hit; 1108 if (rc == ENOENT) 1109 goto miss; 1110 1111 err: 1112 (void) fat_node_put(fn); 1113 async_answer_0(callid, rc); 1114 return rc; 1115 1152 /* 1153 * Our strategy for readdir() is to use the position pointer as 1154 * an index into the array of all dentries. On entry, it points 1155 * to the first unread dentry. If we skip any dentries, we bump 1156 * the position pointer accordingly. 1157 */ 1158 bnum = (pos * sizeof(fat_dentry_t)) / BPS(bs); 1159 while (bnum < nodep->size / BPS(bs)) { 1160 aoff64_t o; 1161 1162 rc = fat_block_get(&b, bs, nodep, bnum, 1163 BLOCK_FLAGS_NONE); 1164 if (rc != EOK) 1165 goto err; 1166 for (o = pos % (BPS(bs) / sizeof(fat_dentry_t)); 1167 o < BPS(bs) / sizeof(fat_dentry_t); 1168 o++, pos++) { 1169 d = ((fat_dentry_t *)b->data) + o; 1170 switch (fat_classify_dentry(d)) { 1171 case FAT_DENTRY_SKIP: 1172 case FAT_DENTRY_FREE: 1173 continue; 1174 case FAT_DENTRY_LAST: 1175 rc = block_put(b); 1176 if (rc != EOK) 1177 goto err; 1178 goto miss; 1179 default: 1180 case FAT_DENTRY_VALID: 1181 fat_dentry_name_get(d, name); 1182 rc = block_put(b); 1183 if (rc != EOK) 1184 goto err; 1185 goto hit; 1186 } 1187 } 1188 rc = block_put(b); 1189 if (rc != EOK) 1190 goto err; 1191 bnum++; 1192 } 1116 1193 miss: 1117 rc = fat_directory_close(&di);1118 if (rc != EOK)1119 goto err;1120 1194 rc = fat_node_put(fn); 1121 1195 async_answer_0(callid, rc != EOK ? rc : ENOENT); … … 1123 1197 return rc != EOK ? rc : ENOENT; 1124 1198 1199 err: 1200 (void) fat_node_put(fn); 1201 async_answer_0(callid, rc); 1202 return rc; 1203 1125 1204 hit: 1126 pos = di.pos; 1127 rc = fat_directory_close(&di); 1128 if (rc != EOK) 1129 goto err; 1130 (void) async_data_read_finalize(callid, name, 1131 str_size(name) + 1); 1205 (void) async_data_read_finalize(callid, name, str_size(name) + 1); 1132 1206 bytes = (pos - spos) + 1; 1133 1207 } … … 1157 1231 return ENOENT; 1158 1232 nodep = FAT_NODE(fn); 1159 1233 1160 1234 ipc_callid_t callid; 1161 1235 size_t len; … … 1173 1247 * but this one greatly simplifies fat_write(). Note that we can afford 1174 1248 * to do this because the client must be ready to handle the return 1175 * value signalizing a smaller number of bytes written. 1176 */ 1249 * value signalizing a smaller number of bytes written. 1250 */ 1177 1251 bytes = min(len, BPS(bs) - pos % BPS(bs)); 1178 1252 if (bytes == BPS(bs)) 1179 1253 flags |= BLOCK_FLAGS_NOREAD; 1180 1254 1181 1255 boundary = ROUND_UP(nodep->size, BPC(bs)); 1182 1256 if (pos < boundary) { … … 1221 1295 */ 1222 1296 unsigned nclsts; 1223 fat_cluster_t mcl, lcl; 1224 1297 fat_cluster_t mcl, lcl; 1298 1225 1299 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs); 1226 1300 /* create an independent chain of nclsts clusters in all FATs */ … … 1306 1380 nodep->size = size; 1307 1381 nodep->dirty = true; /* need to sync node */ 1308 rc = EOK; 1382 rc = EOK; 1309 1383 } else { 1310 1384 /* … … 1327 1401 nodep->size = size; 1328 1402 nodep->dirty = true; /* need to sync node */ 1329 rc = EOK; 1403 rc = EOK; 1330 1404 } 1331 1405 out: … … 1370 1444 if (!fn) 1371 1445 return ENOENT; 1372 1446 1373 1447 fat_node_t *nodep = FAT_NODE(fn); 1374 1448 1375 1449 nodep->dirty = true; 1376 1450 rc = fat_node_sync(nodep); 1377 1451 1378 1452 fat_node_put(fn); 1379 1453 return rc;
Note:
See TracChangeset
for help on using the changeset viewer.