1 /*
2 This file is part of UFFS, the Ultra-low-cost Flash File System.
3
4 Copyright (C) 2005-2009 Ricky Zheng <[email protected]>
5
6 UFFS is free software; you can redistribute it and/or modify it under
7 the GNU Library General Public License as published by the Free Software
8 Foundation; either version 2 of the License, or (at your option) any
9 later version.
10
11 UFFS is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 or GNU Library General Public License, as applicable, for more details.
15
16 You should have received a copy of the GNU General Public License
17 and GNU Library General Public License along with UFFS; if not, write
18 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20
21 As a special exception, if other files instantiate templates or use
22 macros or inline functions from this file, or you compile this file
23 and link it with other works to produce a work based on this file,
24 this file does not by itself cause the resulting work to be covered
25 by the GNU General Public License. However the source code for this
26 file must still be made available in accordance with section (3) of
27 the GNU General Public License v2.
28
29 This exception does not invalidate any other reasons why a work based
30 on this file might be covered by the GNU General Public License.
31 */
32
33 /**
34 * \file static-mem-allocate.c
35 * \brief demostrate how to use static memory allocation. This example use
36 * file emulated NAND flash, one partition only.
37 * \author Ricky Zheng
38 */
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include "uffs_config.h"
44 #include "uffs/uffs_public.h"
45 #include "uffs/uffs_fs.h"
46 #include "uffs/uffs_utils.h"
47 #include "uffs/uffs_core.h"
48 #include "uffs/uffs_mtb.h"
49 #include "cmdline.h"
50 #include "uffs_fileem.h"
51
52 #define PFX "sexp: "
53
54 #if CONFIG_USE_STATIC_MEMORY_ALLOCATOR == 0
main()55 int main()
56 {
57 uffs_Perror(UFFS_MSG_NORMAL, "This example need CONFIG_USE_STATIC_MEMORY_ALLOCATOR = 1");
58 return 0;
59 }
60 #else
61
62 extern struct cli_commandset * get_helper_cmds(void);
63 extern struct cli_commandset * get_test_cmds(void);
64
65 #define PAGE_DATA_SIZE 512
66 #define PAGE_SPARE_SIZE 16
67 #define PAGES_PER_BLOCK 32
68 #define TOTAL_BLOCKS 128
69
70 #define PAGE_SIZE (PAGE_DATA_SIZE + PAGE_SPARE_SIZE)
71 #define BLOCK_DATA_SIZE (PAGES_PER_BLOCK * PAGE_DATA_SIZE)
72 #define TOTAL_DATA_SIZE (TOTAL_BLOCKS * BLOCK_DATA_SIZE)
73 #define BLOCK_SIZE (PAGES_PER_BLOCK * PAGE_SIZE)
74 #define TOTAL_SIZE (BLOCK_SIZE * TOTAL_BLOCKS)
75
76 #define MAX_MOUNT_TABLES 10
77 #define MAX_MOUNT_POINT_NAME 32
78
79 static uffs_Device demo_device = {0};
80 static struct uffs_MountTableEntrySt demo_mount = {
81 &demo_device,
82 0, /* start from block 0 */
83 -1, /* use whole chip */
84 "/", /* mount point */
85 NULL
86 };
87
88 /* static alloc the memory */
89 static int static_buffer_pool[UFFS_STATIC_BUFF_SIZE(PAGES_PER_BLOCK, PAGE_SIZE, TOTAL_BLOCKS) / sizeof(int)];
90
91
setup_storage(struct uffs_StorageAttrSt * attr)92 static void setup_storage(struct uffs_StorageAttrSt *attr)
93 {
94 attr->total_blocks = TOTAL_BLOCKS; /* total blocks */
95 attr->page_data_size = PAGE_DATA_SIZE; /* page data size */
96 attr->spare_size = PAGE_SPARE_SIZE; /* page spare size */
97 attr->pages_per_block = PAGES_PER_BLOCK; /* pages per block */
98 attr->block_status_offs = 4; /* block status offset is 5th byte in spare */
99 attr->ecc_opt = UFFS_ECC_SOFT; /* use UFFS software ecc */
100 attr->layout_opt = UFFS_LAYOUT_UFFS; /* let UFFS do the spare layout */
101 }
102
setup_device(uffs_Device * dev)103 static void setup_device(uffs_Device *dev)
104 {
105 // using file emulator device
106 dev->Init = femu_InitDevice;
107 dev->Release = femu_ReleaseDevice;
108 dev->attr = femu_GetStorage();
109 }
110
init_uffs_fs(void)111 static int init_uffs_fs(void)
112 {
113 struct uffs_MountTableEntrySt *mtbl = &demo_mount;
114
115 /* setup flash storage attributes */
116 setup_storage(femu_GetStorage());
117
118 /* setup memory allocator */
119 uffs_MemSetupStaticAllocator(&mtbl->dev->mem, static_buffer_pool, sizeof(static_buffer_pool));
120
121 /* setup device: init, release, attr */
122 setup_device(mtbl->dev);
123
124 /* register mount table */
125 uffs_RegisterMountTable(mtbl);
126
127 /* mount it */
128 uffs_Mount("/");
129
130 return uffs_InitFileSystemObjects() == U_SUCC ? 0 : -1;
131 }
132
release_uffs_fs(void)133 static int release_uffs_fs(void)
134 {
135 uffs_UnMount("/");
136
137 return uffs_ReleaseFileSystemObjects();
138 }
139
main(int argc,char * argv[])140 int main(int argc, char *argv[])
141 {
142 int ret;
143
144 uffs_SetupDebugOutput(); // setup debug output as early as possible
145
146 ret = init_uffs_fs();
147
148 if (ret != 0) {
149 printf ("Init file system fail: %d\n", ret);
150 return -1;
151 }
152
153 cli_add_commandset(get_helper_cmds());
154 cli_add_commandset(get_test_cmds());
155 cli_main_entry();
156
157 release_uffs_fs();
158
159 return 0;
160 }
161
162 #endif
163
164