Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. |
| 3 | * |
| 4 | * Copyright (C) 2002-2007 Aleph One Ltd. |
| 5 | * for Toby Churchill Ltd and Brightstar Engineering |
| 6 | * |
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * yaffscfg.c The configuration for the "direct" use of yaffs. |
| 16 | * |
| 17 | * This is set up for u-boot. |
| 18 | * |
| 19 | * This version now uses the ydevconfig mechanism to set up partitions. |
| 20 | */ |
| 21 | |
Tom Rini | 739b9dc | 2013-12-05 14:48:39 -0500 | [diff] [blame] | 22 | #include <div64.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 23 | #include <malloc.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 24 | #include <linux/printk.h> |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 25 | |
| 26 | #include <config.h> |
| 27 | #include "nand.h" |
| 28 | #include "yaffscfg.h" |
| 29 | #include "yaffsfs.h" |
| 30 | #include "yaffs_packedtags2.h" |
| 31 | #include "yaffs_mtdif.h" |
| 32 | #include "yaffs_mtdif2.h" |
| 33 | #if 0 |
| 34 | #include <errno.h> |
| 35 | #else |
| 36 | #include "malloc.h" |
| 37 | #endif |
Tom Rini | 3bde7e2 | 2021-09-22 14:50:35 -0400 | [diff] [blame] | 38 | #include <linux/mtd/rawnand.h> |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 39 | |
| 40 | unsigned yaffs_trace_mask = 0x0; /* Disable logging */ |
| 41 | static int yaffs_errno; |
| 42 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 43 | void yaffs_bug_fn(const char *fn, int n) |
| 44 | { |
| 45 | printf("yaffs bug at %s:%d\n", fn, n); |
| 46 | } |
| 47 | |
| 48 | void *yaffsfs_malloc(size_t x) |
| 49 | { |
| 50 | return malloc(x); |
| 51 | } |
| 52 | |
| 53 | void yaffsfs_free(void *x) |
| 54 | { |
| 55 | free(x); |
| 56 | } |
| 57 | |
| 58 | void yaffsfs_SetError(int err) |
| 59 | { |
| 60 | yaffs_errno = err; |
| 61 | } |
| 62 | |
| 63 | int yaffsfs_GetLastError(void) |
| 64 | { |
| 65 | return yaffs_errno; |
| 66 | } |
| 67 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 68 | int yaffsfs_GetError(void) |
| 69 | { |
| 70 | return yaffs_errno; |
| 71 | } |
| 72 | |
| 73 | void yaffsfs_Lock(void) |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | void yaffsfs_Unlock(void) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | __u32 yaffsfs_CurrentTime(void) |
| 82 | { |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | void *yaffs_malloc(size_t size) |
| 87 | { |
| 88 | return malloc(size); |
| 89 | } |
| 90 | |
| 91 | void yaffs_free(void *ptr) |
| 92 | { |
| 93 | free(ptr); |
| 94 | } |
| 95 | |
| 96 | void yaffsfs_LocalInitialisation(void) |
| 97 | { |
| 98 | /* No locking used */ |
| 99 | } |
| 100 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 101 | static const char *yaffs_file_type_str(struct yaffs_stat *stat) |
| 102 | { |
| 103 | switch (stat->st_mode & S_IFMT) { |
| 104 | case S_IFREG: return "regular file"; |
| 105 | case S_IFDIR: return "directory"; |
| 106 | case S_IFLNK: return "symlink"; |
| 107 | default: return "unknown"; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static const char *yaffs_error_str(void) |
| 112 | { |
| 113 | int error = yaffsfs_GetLastError(); |
| 114 | |
| 115 | if (error < 0) |
| 116 | error = -error; |
| 117 | |
| 118 | switch (error) { |
| 119 | case EBUSY: return "Busy"; |
| 120 | case ENODEV: return "No such device"; |
| 121 | case EINVAL: return "Invalid parameter"; |
| 122 | case ENFILE: return "Too many open files"; |
| 123 | case EBADF: return "Bad handle"; |
| 124 | case EACCES: return "Wrong permissions"; |
| 125 | case EXDEV: return "Not on same device"; |
| 126 | case ENOENT: return "No such entry"; |
| 127 | case ENOSPC: return "Device full"; |
| 128 | case EROFS: return "Read only file system"; |
| 129 | case ERANGE: return "Range error"; |
| 130 | case ENOTEMPTY: return "Not empty"; |
| 131 | case ENAMETOOLONG: return "Name too long"; |
| 132 | case ENOMEM: return "Out of memory"; |
| 133 | case EFAULT: return "Fault"; |
| 134 | case EEXIST: return "Name exists"; |
| 135 | case ENOTDIR: return "Not a directory"; |
| 136 | case EISDIR: return "Not permitted on a directory"; |
| 137 | case ELOOP: return "Symlink loop"; |
| 138 | case 0: return "No error"; |
| 139 | default: return "Unknown error"; |
| 140 | } |
| 141 | } |
| 142 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 143 | void cmd_yaffs_tracemask(unsigned set, unsigned mask) |
| 144 | { |
| 145 | if (set) |
| 146 | yaffs_trace_mask = mask; |
| 147 | |
| 148 | printf("yaffs trace mask: %08x\n", yaffs_trace_mask); |
| 149 | } |
| 150 | |
| 151 | static int yaffs_regions_overlap(int a, int b, int x, int y) |
| 152 | { |
| 153 | return (a <= x && x <= b) || |
| 154 | (a <= y && y <= b) || |
| 155 | (x <= a && a <= y) || |
| 156 | (x <= b && b <= y); |
| 157 | } |
| 158 | |
| 159 | void cmd_yaffs_devconfig(char *_mp, int flash_dev, |
| 160 | int start_block, int end_block) |
| 161 | { |
| 162 | struct mtd_info *mtd = NULL; |
| 163 | struct yaffs_dev *dev = NULL; |
| 164 | struct yaffs_dev *chk; |
| 165 | char *mp = NULL; |
| 166 | struct nand_chip *chip; |
| 167 | |
Grygorii Strashko | ac8f8e3 | 2017-06-26 19:12:57 -0500 | [diff] [blame] | 168 | mtd = get_nand_dev_by_index(flash_dev); |
| 169 | if (!mtd) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 170 | pr_err("\nno NAND devices available\n"); |
Grygorii Strashko | ac8f8e3 | 2017-06-26 19:12:57 -0500 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 174 | dev = calloc(1, sizeof(*dev)); |
| 175 | mp = strdup(_mp); |
| 176 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 177 | if (!dev || !mp) { |
| 178 | /* Alloc error */ |
| 179 | printf("Failed to allocate memory\n"); |
| 180 | goto err; |
| 181 | } |
| 182 | |
| 183 | if (flash_dev >= CONFIG_SYS_MAX_NAND_DEVICE) { |
| 184 | printf("Flash device invalid\n"); |
| 185 | goto err; |
| 186 | } |
| 187 | |
| 188 | if (end_block == 0) |
Tom Rini | 739b9dc | 2013-12-05 14:48:39 -0500 | [diff] [blame] | 189 | end_block = lldiv(mtd->size, mtd->erasesize - 1); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 190 | |
| 191 | if (end_block < start_block) { |
| 192 | printf("Bad start/end\n"); |
| 193 | goto err; |
| 194 | } |
| 195 | |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 196 | chip = mtd_to_nand(mtd); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 197 | |
| 198 | /* Check for any conflicts */ |
| 199 | yaffs_dev_rewind(); |
| 200 | while (1) { |
| 201 | chk = yaffs_next_dev(); |
| 202 | if (!chk) |
| 203 | break; |
| 204 | if (strcmp(chk->param.name, mp) == 0) { |
| 205 | printf("Mount point name already used\n"); |
| 206 | goto err; |
| 207 | } |
| 208 | if (chk->driver_context == mtd && |
| 209 | yaffs_regions_overlap( |
| 210 | chk->param.start_block, chk->param.end_block, |
| 211 | start_block, end_block)) { |
| 212 | printf("Region overlaps with partition %s\n", |
| 213 | chk->param.name); |
| 214 | goto err; |
| 215 | } |
| 216 | |
| 217 | } |
| 218 | |
| 219 | /* Seems sane, so configure */ |
| 220 | memset(dev, 0, sizeof(*dev)); |
| 221 | dev->param.name = mp; |
| 222 | dev->driver_context = mtd; |
| 223 | dev->param.start_block = start_block; |
| 224 | dev->param.end_block = end_block; |
| 225 | dev->param.chunks_per_block = mtd->erasesize / mtd->writesize; |
| 226 | dev->param.total_bytes_per_chunk = mtd->writesize; |
| 227 | dev->param.is_yaffs2 = 1; |
| 228 | dev->param.use_nand_ecc = 1; |
| 229 | dev->param.n_reserved_blocks = 5; |
| 230 | if (chip->ecc.layout->oobavail < sizeof(struct yaffs_packed_tags2)) |
| 231 | dev->param.inband_tags = 1; |
| 232 | dev->param.n_caches = 10; |
| 233 | dev->param.write_chunk_tags_fn = nandmtd2_write_chunk_tags; |
| 234 | dev->param.read_chunk_tags_fn = nandmtd2_read_chunk_tags; |
| 235 | dev->param.erase_fn = nandmtd_EraseBlockInNAND; |
| 236 | dev->param.initialise_flash_fn = nandmtd_InitialiseNAND; |
| 237 | dev->param.bad_block_fn = nandmtd2_MarkNANDBlockBad; |
| 238 | dev->param.query_block_fn = nandmtd2_QueryNANDBlock; |
| 239 | |
| 240 | yaffs_add_device(dev); |
| 241 | |
| 242 | printf("Configures yaffs mount %s: dev %d start block %d, end block %d %s\n", |
| 243 | mp, flash_dev, start_block, end_block, |
| 244 | dev->param.inband_tags ? "using inband tags" : ""); |
| 245 | return; |
| 246 | |
| 247 | err: |
| 248 | free(dev); |
| 249 | free(mp); |
| 250 | } |
| 251 | |
| 252 | void cmd_yaffs_dev_ls(void) |
| 253 | { |
| 254 | struct yaffs_dev *dev; |
| 255 | int flash_dev; |
| 256 | int free_space; |
| 257 | |
| 258 | yaffs_dev_rewind(); |
| 259 | |
| 260 | while (1) { |
| 261 | dev = yaffs_next_dev(); |
| 262 | if (!dev) |
| 263 | return; |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 264 | flash_dev = nand_mtd_to_devnum(dev->driver_context); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 265 | printf("%-10s %5d 0x%05x 0x%05x %s", |
| 266 | dev->param.name, flash_dev, |
| 267 | dev->param.start_block, dev->param.end_block, |
| 268 | dev->param.inband_tags ? "using inband tags, " : ""); |
| 269 | |
| 270 | free_space = yaffs_freespace(dev->param.name); |
| 271 | if (free_space < 0) |
| 272 | printf("not mounted\n"); |
| 273 | else |
| 274 | printf("free 0x%x\n", free_space); |
| 275 | |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void make_a_file(char *yaffsName, char bval, int sizeOfFile) |
| 280 | { |
| 281 | int outh; |
| 282 | int i; |
| 283 | unsigned char buffer[100]; |
| 284 | |
| 285 | outh = yaffs_open(yaffsName, |
| 286 | O_CREAT | O_RDWR | O_TRUNC, |
| 287 | S_IREAD | S_IWRITE); |
| 288 | if (outh < 0) { |
| 289 | printf("Error opening file: %d. %s\n", outh, yaffs_error_str()); |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | memset(buffer, bval, 100); |
| 294 | |
| 295 | do { |
| 296 | i = sizeOfFile; |
| 297 | if (i > 100) |
| 298 | i = 100; |
| 299 | sizeOfFile -= i; |
| 300 | |
| 301 | yaffs_write(outh, buffer, i); |
| 302 | |
| 303 | } while (sizeOfFile > 0); |
| 304 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 305 | yaffs_close(outh); |
| 306 | } |
| 307 | |
| 308 | void read_a_file(char *fn) |
| 309 | { |
| 310 | int h; |
| 311 | int i = 0; |
| 312 | unsigned char b; |
| 313 | |
| 314 | h = yaffs_open(fn, O_RDWR, 0); |
| 315 | if (h < 0) { |
| 316 | printf("File not found\n"); |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | while (yaffs_read(h, &b, 1) > 0) { |
| 321 | printf("%02x ", b); |
| 322 | i++; |
| 323 | if (i > 32) { |
| 324 | printf("\n"); |
Masahiro Yamada | 622b7b3 | 2017-06-13 15:17:28 +0900 | [diff] [blame] | 325 | i = 0; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | printf("\n"); |
| 329 | yaffs_close(h); |
| 330 | } |
| 331 | |
| 332 | void cmd_yaffs_mount(char *mp) |
| 333 | { |
| 334 | int retval = yaffs_mount(mp); |
| 335 | if (retval < 0) |
| 336 | printf("Error mounting %s, return value: %d, %s\n", mp, |
| 337 | yaffsfs_GetError(), yaffs_error_str()); |
| 338 | } |
| 339 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 340 | void cmd_yaffs_umount(char *mp) |
| 341 | { |
| 342 | if (yaffs_unmount(mp) == -1) |
| 343 | printf("Error umounting %s, return value: %d, %s\n", mp, |
| 344 | yaffsfs_GetError(), yaffs_error_str()); |
| 345 | } |
| 346 | |
| 347 | void cmd_yaffs_write_file(char *yaffsName, char bval, int sizeOfFile) |
| 348 | { |
| 349 | make_a_file(yaffsName, bval, sizeOfFile); |
| 350 | } |
| 351 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 352 | void cmd_yaffs_read_file(char *fn) |
| 353 | { |
| 354 | read_a_file(fn); |
| 355 | } |
| 356 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 357 | void cmd_yaffs_mread_file(char *fn, char *addr) |
| 358 | { |
| 359 | int h; |
| 360 | struct yaffs_stat s; |
| 361 | |
| 362 | yaffs_stat(fn, &s); |
| 363 | |
| 364 | printf("Copy %s to 0x%p... ", fn, addr); |
| 365 | h = yaffs_open(fn, O_RDWR, 0); |
| 366 | if (h < 0) { |
| 367 | printf("File not found\n"); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | yaffs_read(h, addr, (int)s.st_size); |
| 372 | printf("\t[DONE]\n"); |
| 373 | |
| 374 | yaffs_close(h); |
| 375 | } |
| 376 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 377 | void cmd_yaffs_mwrite_file(char *fn, char *addr, int size) |
| 378 | { |
| 379 | int outh; |
| 380 | |
| 381 | outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE); |
| 382 | if (outh < 0) |
| 383 | printf("Error opening file: %d, %s\n", outh, yaffs_error_str()); |
| 384 | |
| 385 | yaffs_write(outh, addr, size); |
| 386 | |
| 387 | yaffs_close(outh); |
| 388 | } |
| 389 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 390 | void cmd_yaffs_ls(const char *mountpt, int longlist) |
| 391 | { |
| 392 | int i; |
| 393 | yaffs_DIR *d; |
| 394 | struct yaffs_dirent *de; |
| 395 | struct yaffs_stat stat; |
| 396 | char tempstr[255]; |
| 397 | |
| 398 | d = yaffs_opendir(mountpt); |
| 399 | |
| 400 | if (!d) { |
| 401 | printf("opendir failed, %s\n", yaffs_error_str()); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | for (i = 0; (de = yaffs_readdir(d)) != NULL; i++) { |
| 406 | if (longlist) { |
| 407 | sprintf(tempstr, "%s/%s", mountpt, de->d_name); |
| 408 | yaffs_lstat(tempstr, &stat); |
| 409 | printf("%-25s\t%7ld", |
| 410 | de->d_name, |
| 411 | (long)stat.st_size); |
| 412 | printf(" %5d %s\n", |
| 413 | stat.st_ino, |
| 414 | yaffs_file_type_str(&stat)); |
| 415 | } else { |
| 416 | printf("%s\n", de->d_name); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | yaffs_closedir(d); |
| 421 | } |
| 422 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 423 | void cmd_yaffs_mkdir(const char *dir) |
| 424 | { |
| 425 | int retval = yaffs_mkdir(dir, 0); |
| 426 | |
| 427 | if (retval < 0) |
| 428 | printf("yaffs_mkdir returning error: %d, %s\n", |
| 429 | retval, yaffs_error_str()); |
| 430 | } |
| 431 | |
| 432 | void cmd_yaffs_rmdir(const char *dir) |
| 433 | { |
| 434 | int retval = yaffs_rmdir(dir); |
| 435 | |
| 436 | if (retval < 0) |
| 437 | printf("yaffs_rmdir returning error: %d, %s\n", |
| 438 | retval, yaffs_error_str()); |
| 439 | } |
| 440 | |
| 441 | void cmd_yaffs_rm(const char *path) |
| 442 | { |
| 443 | int retval = yaffs_unlink(path); |
| 444 | |
| 445 | if (retval < 0) |
| 446 | printf("yaffs_unlink returning error: %d, %s\n", |
| 447 | retval, yaffs_error_str()); |
| 448 | } |
| 449 | |
| 450 | void cmd_yaffs_mv(const char *oldPath, const char *newPath) |
| 451 | { |
| 452 | int retval = yaffs_rename(newPath, oldPath); |
| 453 | |
| 454 | if (retval < 0) |
| 455 | printf("yaffs_unlink returning error: %d, %s\n", |
| 456 | retval, yaffs_error_str()); |
| 457 | } |