chfs_mkfs.c revision 1.1 1 1.1 ttoth /*-
2 1.1 ttoth * Copyright (c) 2012 Department of Software Engineering,
3 1.1 ttoth * University of Szeged, Hungary
4 1.1 ttoth * Copyright (c) 2012 Tamas Toth <ttoth (at) inf.u-szeged.hu>
5 1.1 ttoth * All rights reserved.
6 1.1 ttoth *
7 1.1 ttoth * This code is derived from software contributed to The NetBSD Foundation
8 1.1 ttoth * by the Department of Software Engineering, University of Szeged, Hungary
9 1.1 ttoth *
10 1.1 ttoth * Redistribution and use in source and binary forms, with or without
11 1.1 ttoth * modification, are permitted provided that the following conditions
12 1.1 ttoth * are met:
13 1.1 ttoth * 1. Redistributions of source code must retain the above copyright
14 1.1 ttoth * notice, this list of conditions and the following disclaimer.
15 1.1 ttoth * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ttoth * notice, this list of conditions and the following disclaimer in the
17 1.1 ttoth * documentation and/or other materials provided with the distribution.
18 1.1 ttoth *
19 1.1 ttoth * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 ttoth * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 ttoth * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 ttoth * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 ttoth * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 1.1 ttoth * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.1 ttoth * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 1.1 ttoth * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 1.1 ttoth * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 ttoth * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 ttoth * SUCH DAMAGE.
30 1.1 ttoth */
31 1.1 ttoth
32 1.1 ttoth #if HAVE_NBTOOL_CONFIG_H
33 1.1 ttoth #include "nbtool_config.h"
34 1.1 ttoth #endif
35 1.1 ttoth
36 1.1 ttoth #include <sys/param.h>
37 1.1 ttoth #include <sys/stat.h>
38 1.1 ttoth
39 1.1 ttoth #include <assert.h>
40 1.1 ttoth #include <fcntl.h>
41 1.1 ttoth #include <stdio.h>
42 1.1 ttoth #include <stdlib.h>
43 1.1 ttoth #include <string.h>
44 1.1 ttoth #include <zlib.h>
45 1.1 ttoth
46 1.1 ttoth #include "makefs.h"
47 1.1 ttoth #include "chfs_makefs.h"
48 1.1 ttoth
49 1.1 ttoth #include "media.h"
50 1.1 ttoth #include "ebh.h"
51 1.1 ttoth
52 1.1 ttoth #include "chfs/chfs_mkfs.h"
53 1.1 ttoth
54 1.1 ttoth static uint32_t img_ofs = 0;
55 1.1 ttoth static uint64_t version = 0;
56 1.1 ttoth static uint64_t max_serial = 0;
57 1.1 ttoth static int lebnumber = 0;
58 1.1 ttoth
59 1.1 ttoth static const unsigned char ffbuf[16] = {
60 1.1 ttoth 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61 1.1 ttoth 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
62 1.1 ttoth };
63 1.1 ttoth
64 1.1 ttoth static void
65 1.1 ttoth buf_write(fsinfo_t *fsopts, const void *buf, size_t len)
66 1.1 ttoth {
67 1.1 ttoth ssize_t retval;
68 1.1 ttoth const char *charbuf = buf;
69 1.1 ttoth
70 1.1 ttoth while (len > 0) {
71 1.1 ttoth retval = write(fsopts->fd, charbuf, len);
72 1.1 ttoth
73 1.1 ttoth if (retval == -1) {
74 1.1 ttoth err(EXIT_FAILURE, "ERROR while writing");
75 1.1 ttoth }
76 1.1 ttoth
77 1.1 ttoth len -= retval;
78 1.1 ttoth charbuf += retval;
79 1.1 ttoth img_ofs += retval;
80 1.1 ttoth }
81 1.1 ttoth }
82 1.1 ttoth
83 1.1 ttoth void
84 1.1 ttoth padblock(fsinfo_t *fsopts)
85 1.1 ttoth {
86 1.1 ttoth while (img_ofs % chfs_opts.eraseblock) {
87 1.1 ttoth buf_write(fsopts, ffbuf, MIN(sizeof(ffbuf),
88 1.1 ttoth chfs_opts.eraseblock - (img_ofs % chfs_opts.eraseblock)));
89 1.1 ttoth }
90 1.1 ttoth }
91 1.1 ttoth
92 1.1 ttoth static void
93 1.1 ttoth padword(fsinfo_t *fsopts)
94 1.1 ttoth {
95 1.1 ttoth if (img_ofs % 4) {
96 1.1 ttoth buf_write(fsopts, ffbuf, 4 - img_ofs % 4);
97 1.1 ttoth }
98 1.1 ttoth }
99 1.1 ttoth
100 1.1 ttoth static void
101 1.1 ttoth pad_block_if_less_than(fsinfo_t *fsopts, int req)
102 1.1 ttoth {
103 1.1 ttoth if ((img_ofs % chfs_opts.eraseblock) + req > chfs_opts.eraseblock) {
104 1.1 ttoth padblock(fsopts);
105 1.1 ttoth write_eb_header(fsopts);
106 1.1 ttoth }
107 1.1 ttoth }
108 1.1 ttoth
109 1.1 ttoth void
110 1.1 ttoth write_eb_header(fsinfo_t *fsopts)
111 1.1 ttoth {
112 1.1 ttoth chfs_opt_t *chfs_opts;
113 1.1 ttoth struct chfs_eb_hdr ebhdr;
114 1.1 ttoth
115 1.1 ttoth chfs_opts = fsopts->fs_specific;
116 1.1 ttoth
117 1.1 ttoth char buf[chfs_opts->pagesize];
118 1.1 ttoth
119 1.1 ttoth memset(buf, 0xFF, sizeof(buf));
120 1.1 ttoth
121 1.1 ttoth ebhdr.ec_hdr.magic = htole32(CHFS_MAGIC_BITMASK);
122 1.1 ttoth ebhdr.ec_hdr.erase_cnt = htole32(1);
123 1.1 ttoth ebhdr.ec_hdr.crc_ec = htole32(crc32(0, (uint8_t *)&ebhdr.ec_hdr + 8, 4));
124 1.1 ttoth
125 1.1 ttoth memcpy(&buf, &ebhdr.ec_hdr, CHFS_EB_EC_HDR_SIZE);
126 1.1 ttoth
127 1.1 ttoth buf_write(fsopts, &buf, chfs_opts->pagesize);
128 1.1 ttoth
129 1.1 ttoth memset(buf, 0xFF, chfs_opts->pagesize);
130 1.1 ttoth
131 1.1 ttoth if (chfs_opts->mediatype == TYPE_NAND) {
132 1.1 ttoth ebhdr.u.nand_hdr.lid = htole32(lebnumber++);
133 1.1 ttoth ebhdr.u.nand_hdr.serial = htole64(++(max_serial));
134 1.1 ttoth ebhdr.u.nand_hdr.crc = htole32(crc32(0,
135 1.1 ttoth (uint8_t *)&ebhdr.u.nand_hdr + 4, CHFS_EB_HDR_NAND_SIZE - 4));
136 1.1 ttoth memcpy(&buf, &ebhdr.u.nand_hdr, CHFS_EB_HDR_NAND_SIZE);
137 1.1 ttoth } else {
138 1.1 ttoth ebhdr.u.nor_hdr.lid = htole32(lebnumber++);
139 1.1 ttoth ebhdr.u.nor_hdr.crc = htole32(crc32(0, (uint8_t *)&ebhdr.u.nor_hdr + 4,
140 1.1 ttoth CHFS_EB_HDR_NOR_SIZE - 4));
141 1.1 ttoth memcpy(&buf, &ebhdr.u.nor_hdr, CHFS_EB_HDR_NOR_SIZE);
142 1.1 ttoth }
143 1.1 ttoth
144 1.1 ttoth buf_write(fsopts, &buf, chfs_opts->pagesize);
145 1.1 ttoth }
146 1.1 ttoth
147 1.1 ttoth void
148 1.1 ttoth write_vnode(fsinfo_t *fsopts, fsnode *node)
149 1.1 ttoth {
150 1.1 ttoth struct chfs_flash_vnode fvnode;
151 1.1 ttoth memset(&fvnode, 0, sizeof(fvnode));
152 1.1 ttoth
153 1.1 ttoth fvnode.magic = htole16(CHFS_FS_MAGIC_BITMASK);
154 1.1 ttoth fvnode.type = htole16(CHFS_NODETYPE_VNODE);
155 1.1 ttoth fvnode.length = htole32(CHFS_PAD(sizeof(fvnode)));
156 1.1 ttoth fvnode.hdr_crc = htole32(crc32(0, (uint8_t *)&fvnode,
157 1.1 ttoth CHFS_NODE_HDR_SIZE - 4));
158 1.1 ttoth fvnode.vno = htole64(node->inode->ino);
159 1.1 ttoth fvnode.version = htole64(version++);
160 1.1 ttoth fvnode.mode = htole32(node->inode->st.st_mode);
161 1.1 ttoth fvnode.dn_size = htole32(node->inode->st.st_size);
162 1.1 ttoth fvnode.atime = htole32(node->inode->st.st_atime);
163 1.1 ttoth fvnode.ctime = htole32(node->inode->st.st_ctime);
164 1.1 ttoth fvnode.mtime = htole32(node->inode->st.st_mtime);
165 1.1 ttoth fvnode.gid = htole32(node->inode->st.st_uid);
166 1.1 ttoth fvnode.uid = htole32(node->inode->st.st_gid);
167 1.1 ttoth fvnode.node_crc = htole32(crc32(0, (uint8_t *)&fvnode,
168 1.1 ttoth sizeof(fvnode) - 4));
169 1.1 ttoth
170 1.1 ttoth pad_block_if_less_than(fsopts, sizeof(fvnode));
171 1.1 ttoth buf_write(fsopts, &fvnode, sizeof(fvnode));
172 1.1 ttoth padword(fsopts);
173 1.1 ttoth }
174 1.1 ttoth
175 1.1 ttoth void
176 1.1 ttoth write_dirent(fsinfo_t *fsopts, fsnode *node)
177 1.1 ttoth {
178 1.1 ttoth struct chfs_flash_dirent_node fdirent;
179 1.1 ttoth char *name = malloc(sizeof(char) * strlen(node->name));
180 1.1 ttoth
181 1.1 ttoth if (name == NULL) {
182 1.1 ttoth err(EXIT_FAILURE, "ERROR memory allocation failed");
183 1.1 ttoth }
184 1.1 ttoth
185 1.1 ttoth memset(&fdirent, 0, sizeof(fdirent));
186 1.1 ttoth memcpy(name, node->name, strlen(node->name));
187 1.1 ttoth
188 1.1 ttoth fdirent.magic = htole16(CHFS_FS_MAGIC_BITMASK);
189 1.1 ttoth fdirent.type = htole16(CHFS_NODETYPE_DIRENT);
190 1.1 ttoth fdirent.length = htole32(CHFS_PAD(sizeof(fdirent) + strlen(name)));
191 1.1 ttoth fdirent.hdr_crc = htole32(crc32(0, (uint8_t *)&fdirent,
192 1.1 ttoth CHFS_NODE_HDR_SIZE - 4));
193 1.1 ttoth fdirent.vno = htole64(node->inode->ino);
194 1.1 ttoth if (node->parent != NULL) {
195 1.1 ttoth fdirent.pvno = htole64(node->parent->inode->ino);
196 1.1 ttoth } else {
197 1.1 ttoth fdirent.pvno = htole64(node->inode->ino);
198 1.1 ttoth }
199 1.1 ttoth
200 1.1 ttoth fdirent.version = htole64(version++);
201 1.1 ttoth fdirent.mctime = htole32(node->inode->st.st_mtimensec);
202 1.1 ttoth fdirent.nsize = htole32(strlen(name));
203 1.1 ttoth fdirent.dtype = htole32(IFTOCHT(node->type & S_IFMT));
204 1.1 ttoth fdirent.name_crc = htole32(crc32(0, (uint8_t *)name, fdirent.nsize));
205 1.1 ttoth fdirent.node_crc = htole32(crc32(0, (uint8_t *)&fdirent,
206 1.1 ttoth sizeof(fdirent) - 4));
207 1.1 ttoth
208 1.1 ttoth pad_block_if_less_than(fsopts, sizeof(fdirent) + fdirent.nsize);
209 1.1 ttoth buf_write(fsopts, &fdirent, sizeof(fdirent));
210 1.1 ttoth buf_write(fsopts, name, fdirent.nsize);
211 1.1 ttoth padword(fsopts);
212 1.1 ttoth }
213 1.1 ttoth
214 1.1 ttoth void
215 1.1 ttoth write_file(fsinfo_t *fsopts, fsnode *node, const char *dir)
216 1.1 ttoth {
217 1.1 ttoth int fd;
218 1.1 ttoth ssize_t len;
219 1.1 ttoth char *name = node->name;
220 1.1 ttoth chfs_opt_t *chfs_opts;
221 1.1 ttoth unsigned char *buf;
222 1.1 ttoth uint32_t fileofs = 0;
223 1.1 ttoth
224 1.1 ttoth chfs_opts = fsopts->fs_specific;
225 1.1 ttoth buf = malloc(chfs_opts->pagesize);
226 1.1 ttoth
227 1.1 ttoth if (buf == NULL) {
228 1.1 ttoth err(EXIT_FAILURE, "ERROR memory allocation failed");
229 1.1 ttoth }
230 1.1 ttoth
231 1.1 ttoth if (node->type == S_IFREG || node->type == S_IFSOCK) {
232 1.1 ttoth char *longname;
233 1.1 ttoth asprintf(&longname, "%s/%s", dir, name);
234 1.1 ttoth
235 1.1 ttoth fd = open(longname, O_RDONLY, 0444);
236 1.1 ttoth
237 1.1 ttoth while ((len = read(fd, buf, chfs_opts->pagesize))) {
238 1.1 ttoth if (len < 0) {
239 1.1 ttoth warn("ERROR while reading %s", longname);
240 1.1 ttoth free(buf);
241 1.1 ttoth close(fd);
242 1.1 ttoth return;
243 1.1 ttoth }
244 1.1 ttoth
245 1.1 ttoth write_data(fsopts, node, buf, len, fileofs);
246 1.1 ttoth fileofs += len;
247 1.1 ttoth }
248 1.1 ttoth close(fd);
249 1.1 ttoth } else if (node->type == S_IFLNK) {
250 1.1 ttoth len = strlen(node->symlink);
251 1.1 ttoth memcpy(buf, node->symlink, len);
252 1.1 ttoth write_data(fsopts, node, buf, len, 0);
253 1.1 ttoth } else if (node->type == S_IFCHR || node->type == S_IFBLK ||
254 1.1 ttoth node->type == S_IFIFO) {
255 1.1 ttoth len = sizeof(dev_t);
256 1.1 ttoth memcpy(buf, &(node->inode->st.st_rdev), len);
257 1.1 ttoth write_data(fsopts, node, buf, len, 0);
258 1.1 ttoth }
259 1.1 ttoth
260 1.1 ttoth free(buf);
261 1.1 ttoth }
262 1.1 ttoth
263 1.1 ttoth void
264 1.1 ttoth write_data(fsinfo_t *fsopts, fsnode *node, unsigned char *buf, size_t len,
265 1.1 ttoth uint32_t ofs)
266 1.1 ttoth {
267 1.1 ttoth struct chfs_flash_data_node fdata;
268 1.1 ttoth memset(&fdata, 0, sizeof(fdata));
269 1.1 ttoth
270 1.1 ttoth if (len == 0) {
271 1.1 ttoth return;
272 1.1 ttoth }
273 1.1 ttoth
274 1.1 ttoth pad_block_if_less_than(fsopts, sizeof(fdata) + len);
275 1.1 ttoth
276 1.1 ttoth fdata.magic = htole16(CHFS_FS_MAGIC_BITMASK);
277 1.1 ttoth fdata.type = htole16(CHFS_NODETYPE_DATA);
278 1.1 ttoth fdata.length = htole32(CHFS_PAD(sizeof(fdata) + len));
279 1.1 ttoth fdata.hdr_crc = htole32(crc32(0, (uint8_t *)&fdata,
280 1.1 ttoth CHFS_NODE_HDR_SIZE - 4));
281 1.1 ttoth fdata.vno = htole64(node->inode->ino);
282 1.1 ttoth fdata.data_length = htole32(len);
283 1.1 ttoth fdata.offset = htole32(ofs);
284 1.1 ttoth fdata.data_crc = htole32(crc32(0, (uint8_t *)buf, len));
285 1.1 ttoth fdata.node_crc = htole32(crc32(0, (uint8_t *)&fdata, sizeof(fdata) - 4));
286 1.1 ttoth
287 1.1 ttoth buf_write(fsopts, &fdata, sizeof(fdata));
288 1.1 ttoth buf_write(fsopts, buf, len);
289 1.1 ttoth padword(fsopts);
290 1.1 ttoth }
291