[dccf721] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup fs
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file mkfat.c
|
---|
| 35 | * @brief Tool for creating new FAT file systems.
|
---|
| 36 | *
|
---|
[602c3d8c] | 37 | * Currently we can create 12/16/32-bit FAT.
|
---|
[dccf721] | 38 | */
|
---|
| 39 |
|
---|
[f4ae95a] | 40 | #include <ctype.h>
|
---|
[dccf721] | 41 | #include <stdio.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 43 | #include <stdint.h>
|
---|
[f73b291] | 44 | #include <block.h>
|
---|
[dccf721] | 45 | #include <mem.h>
|
---|
[15f3c3f] | 46 | #include <loc.h>
|
---|
[dccf721] | 47 | #include <byteorder.h>
|
---|
[fb6f1a5] | 48 | #include <inttypes.h>
|
---|
[dccf721] | 49 | #include <errno.h>
|
---|
[1d6dd2a] | 50 | #include <str.h>
|
---|
[dccf721] | 51 | #include "fat.h"
|
---|
[f4ae95a] | 52 | #include "fat_dentry.h"
|
---|
[dccf721] | 53 |
|
---|
| 54 | #define NAME "mkfat"
|
---|
| 55 |
|
---|
[f4ae95a] | 56 | #define LABEL_NONAME "NO NAME"
|
---|
| 57 |
|
---|
[dccf721] | 58 | /** Divide and round up. */
|
---|
| 59 | #define div_round_up(a, b) (((a) + (b) - 1) / (b))
|
---|
| 60 |
|
---|
[602c3d8c] | 61 | /** Default file-system parameters */
|
---|
[dccf721] | 62 | enum {
|
---|
[602c3d8c] | 63 | default_sector_size = 512,
|
---|
| 64 | default_sectors_per_cluster = 4,
|
---|
| 65 | default_fat_count = 2,
|
---|
| 66 | default_reserved_clusters = 2,
|
---|
| 67 | default_media_descriptor = 0xF8 /**< fixed disk */
|
---|
[dccf721] | 68 | };
|
---|
| 69 |
|
---|
| 70 | /** Configurable file-system parameters */
|
---|
| 71 | typedef struct fat_cfg {
|
---|
[602c3d8c] | 72 | int fat_type; /* FAT12 = 12, FAT16 = 16, FAT32 = 32 */
|
---|
| 73 | size_t sector_size;
|
---|
[dccf721] | 74 | uint32_t total_sectors;
|
---|
| 75 | uint16_t root_ent_max;
|
---|
[602c3d8c] | 76 | uint32_t addt_res_sectors;
|
---|
| 77 | uint8_t sectors_per_cluster;
|
---|
[dccf721] | 78 |
|
---|
| 79 | uint16_t reserved_sectors;
|
---|
[602c3d8c] | 80 | uint32_t rootdir_sectors;
|
---|
[dccf721] | 81 | uint32_t fat_sectors;
|
---|
[602c3d8c] | 82 | uint32_t total_clusters;
|
---|
| 83 | uint8_t fat_count;
|
---|
[f4ae95a] | 84 | const char *label;
|
---|
[602c3d8c] | 85 | } fat_cfg_t;
|
---|
[dccf721] | 86 |
|
---|
[9245413] | 87 | static void syntax_print(void);
|
---|
| 88 |
|
---|
[b7fd2a0] | 89 | static errno_t fat_params_compute(struct fat_cfg *cfg);
|
---|
| 90 | static errno_t fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id);
|
---|
[602c3d8c] | 91 | static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs);
|
---|
[dccf721] | 92 |
|
---|
| 93 | int main(int argc, char **argv)
|
---|
| 94 | {
|
---|
| 95 | struct fat_cfg cfg;
|
---|
| 96 |
|
---|
[b7fd2a0] | 97 | errno_t rc;
|
---|
[dccf721] | 98 | char *dev_path;
|
---|
[15f3c3f] | 99 | service_id_t service_id;
|
---|
[dccf721] | 100 | char *endptr;
|
---|
[ed903174] | 101 | aoff64_t dev_nblocks;
|
---|
[dccf721] | 102 |
|
---|
[602c3d8c] | 103 | cfg.sector_size = default_sector_size;
|
---|
| 104 | cfg.sectors_per_cluster = default_sectors_per_cluster;
|
---|
| 105 | cfg.fat_count = default_fat_count;
|
---|
[dccf721] | 106 | cfg.total_sectors = 0;
|
---|
| 107 | cfg.addt_res_sectors = 0;
|
---|
| 108 | cfg.root_ent_max = 128;
|
---|
[ed9bf14] | 109 | cfg.fat_type = FATAUTO;
|
---|
[f4ae95a] | 110 | cfg.label = NULL;
|
---|
[dccf721] | 111 |
|
---|
| 112 | if (argc < 2) {
|
---|
[9245413] | 113 | printf(NAME ": Error, argument missing.\n");
|
---|
| 114 | syntax_print();
|
---|
[dccf721] | 115 | return 1;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[1433ecda] | 118 | --argc;
|
---|
| 119 | ++argv;
|
---|
[dccf721] | 120 |
|
---|
[2456fd0] | 121 | while (*argv[0] == '-') {
|
---|
| 122 | if (str_cmp(*argv, "--size") == 0) {
|
---|
[1433ecda] | 123 | --argc;
|
---|
| 124 | ++argv;
|
---|
[2456fd0] | 125 | if (*argv == NULL) {
|
---|
| 126 | printf(NAME ": Error, argument missing.\n");
|
---|
| 127 | syntax_print();
|
---|
| 128 | return 1;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | cfg.total_sectors = strtol(*argv, &endptr, 10);
|
---|
| 132 | if (*endptr != '\0') {
|
---|
| 133 | printf(NAME ": Error, invalid argument.\n");
|
---|
| 134 | syntax_print();
|
---|
| 135 | return 1;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[1433ecda] | 138 | --argc;
|
---|
| 139 | ++argv;
|
---|
[dccf721] | 140 | }
|
---|
| 141 |
|
---|
[2456fd0] | 142 | if (str_cmp(*argv, "--type") == 0) {
|
---|
[1433ecda] | 143 | --argc;
|
---|
| 144 | ++argv;
|
---|
[2456fd0] | 145 | if (*argv == NULL) {
|
---|
| 146 | printf(NAME ": Error, argument missing.\n");
|
---|
| 147 | syntax_print();
|
---|
| 148 | return 1;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | cfg.fat_type = strtol(*argv, &endptr, 10);
|
---|
| 152 | if (*endptr != '\0') {
|
---|
| 153 | printf(NAME ": Error, invalid argument.\n");
|
---|
| 154 | syntax_print();
|
---|
| 155 | return 1;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[1433ecda] | 158 | --argc;
|
---|
| 159 | ++argv;
|
---|
[602c3d8c] | 160 | }
|
---|
| 161 |
|
---|
[2456fd0] | 162 | if (str_cmp(*argv, "--label") == 0) {
|
---|
[1433ecda] | 163 | --argc;
|
---|
| 164 | ++argv;
|
---|
[2456fd0] | 165 | if (*argv == NULL) {
|
---|
| 166 | printf(NAME ": Error, argument missing.\n");
|
---|
| 167 | syntax_print();
|
---|
| 168 | return 1;
|
---|
| 169 | }
|
---|
[602c3d8c] | 170 |
|
---|
[2456fd0] | 171 | cfg.label = *argv;
|
---|
[602c3d8c] | 172 |
|
---|
[1433ecda] | 173 | --argc;
|
---|
| 174 | ++argv;
|
---|
[f4ae95a] | 175 | }
|
---|
| 176 |
|
---|
[2456fd0] | 177 | if (str_cmp(*argv, "-") == 0) {
|
---|
[1433ecda] | 178 | --argc;
|
---|
| 179 | ++argv;
|
---|
[2456fd0] | 180 | break;
|
---|
| 181 | }
|
---|
[f4ae95a] | 182 | }
|
---|
| 183 |
|
---|
[dccf721] | 184 | if (argc != 1) {
|
---|
[9245413] | 185 | printf(NAME ": Error, unexpected argument.\n");
|
---|
| 186 | syntax_print();
|
---|
[dccf721] | 187 | return 1;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | dev_path = *argv;
|
---|
[602c3d8c] | 191 | printf("Device: %s\n", dev_path);
|
---|
[dccf721] | 192 |
|
---|
[15f3c3f] | 193 | rc = loc_service_get_id(dev_path, &service_id, 0);
|
---|
[dccf721] | 194 | if (rc != EOK) {
|
---|
[9245413] | 195 | printf(NAME ": Error resolving device `%s'.\n", dev_path);
|
---|
[dccf721] | 196 | return 2;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[fc22069] | 199 | rc = block_init(service_id, 2048);
|
---|
[dccf721] | 200 | if (rc != EOK) {
|
---|
| 201 | printf(NAME ": Error initializing libblock.\n");
|
---|
| 202 | return 2;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[375ab5e] | 205 | rc = block_get_bsize(service_id, &cfg.sector_size);
|
---|
[dccf721] | 206 | if (rc != EOK) {
|
---|
| 207 | printf(NAME ": Error determining device block size.\n");
|
---|
| 208 | return 2;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[15f3c3f] | 211 | rc = block_get_nblocks(service_id, &dev_nblocks);
|
---|
[08232ee] | 212 | if (rc != EOK) {
|
---|
| 213 | printf(NAME ": Warning, failed to obtain block device size.\n");
|
---|
| 214 | } else {
|
---|
[ed903174] | 215 | printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
|
---|
[1ccafee] | 216 | dev_nblocks);
|
---|
[81e20c7] | 217 | if (!cfg.total_sectors || dev_nblocks < cfg.total_sectors)
|
---|
[c80100f] | 218 | cfg.total_sectors = dev_nblocks;
|
---|
[08232ee] | 219 | }
|
---|
| 220 |
|
---|
[dccf721] | 221 | if (cfg.total_sectors == 0) {
|
---|
| 222 | printf(NAME ": Error. You must specify filesystem size.\n");
|
---|
| 223 | return 1;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[ed9bf14] | 226 | if (cfg.fat_type != FATAUTO && cfg.fat_type != FAT12 && cfg.fat_type != FAT16 &&
|
---|
| 227 | cfg.fat_type != FAT32) {
|
---|
[462b418] | 228 | printf(NAME ": Error. Unknown FAT type.\n");
|
---|
| 229 | return 2;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[a927398] | 232 | printf(NAME ": Creating FAT filesystem on device %s.\n", dev_path);
|
---|
[dccf721] | 233 |
|
---|
[602c3d8c] | 234 | rc = fat_params_compute(&cfg);
|
---|
[dccf721] | 235 | if (rc != EOK) {
|
---|
| 236 | printf(NAME ": Invalid file-system parameters.\n");
|
---|
| 237 | return 2;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[a927398] | 240 | printf(NAME ": Filesystem type FAT%d.\n", cfg.fat_type);
|
---|
| 241 |
|
---|
[375ab5e] | 242 | rc = fat_blocks_write(&cfg, service_id);
|
---|
[dccf721] | 243 | if (rc != EOK) {
|
---|
| 244 | printf(NAME ": Error writing device.\n");
|
---|
| 245 | return 2;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[15f3c3f] | 248 | block_fini(service_id);
|
---|
[dccf721] | 249 | printf("Success.\n");
|
---|
| 250 |
|
---|
| 251 | return 0;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[9245413] | 254 | static void syntax_print(void)
|
---|
| 255 | {
|
---|
[2456fd0] | 256 | printf("syntax: mkfat [<options>...] <device_name>\n");
|
---|
| 257 | printf("options:\n"
|
---|
| 258 | "\t--size <sectors> Filesystem size, overrides device size\n"
|
---|
| 259 | "\t--type 12|16|32 FAT type (auto-detected by default)\n"
|
---|
| 260 | "\t--label <label> Volume label\n");
|
---|
[f4ae95a] | 261 | }
|
---|
| 262 |
|
---|
[b7fd2a0] | 263 | static errno_t fat_label_encode(void *dest, const char *src)
|
---|
[f4ae95a] | 264 | {
|
---|
| 265 | int i;
|
---|
| 266 | const char *sp;
|
---|
| 267 | uint8_t *dp;
|
---|
| 268 |
|
---|
| 269 | i = 0;
|
---|
| 270 | sp = src;
|
---|
| 271 | dp = (uint8_t *)dest;
|
---|
| 272 |
|
---|
| 273 | while (*sp != '\0' && i < FAT_VOLLABEL_LEN) {
|
---|
| 274 | if (!ascii_check(*sp))
|
---|
| 275 | return EINVAL;
|
---|
| 276 | if (dp != NULL)
|
---|
| 277 | dp[i] = toupper(*sp);
|
---|
[1433ecda] | 278 | ++i;
|
---|
| 279 | ++sp;
|
---|
[f4ae95a] | 280 | }
|
---|
| 281 |
|
---|
| 282 | while (i < FAT_VOLLABEL_LEN) {
|
---|
| 283 | if (dp != NULL)
|
---|
| 284 | dp[i] = FAT_PAD;
|
---|
| 285 | ++i;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | return EOK;
|
---|
[9245413] | 289 | }
|
---|
| 290 |
|
---|
[dccf721] | 291 | /** Derive sizes of different filesystem structures.
|
---|
| 292 | *
|
---|
| 293 | * This function concentrates all the different computations of FAT
|
---|
| 294 | * file system params.
|
---|
| 295 | */
|
---|
[b7fd2a0] | 296 | static errno_t fat_params_compute(struct fat_cfg *cfg)
|
---|
[dccf721] | 297 | {
|
---|
| 298 | uint32_t fat_bytes;
|
---|
[a927398] | 299 | uint32_t non_data_sectors_lb_16;
|
---|
[dccf721] | 300 | uint32_t non_data_sectors_lb;
|
---|
[a927398] | 301 | uint32_t rd_sectors;
|
---|
| 302 | uint32_t tot_clust_16;
|
---|
[dccf721] | 303 |
|
---|
| 304 | /*
|
---|
[26ad20b] | 305 | * Make a conservative guess on the FAT size needed for the file
|
---|
| 306 | * system. The optimum could be potentially smaller since we
|
---|
| 307 | * do not subtract size of the FAT itself when computing the
|
---|
[a927398] | 308 | * size of the data region. Also the root dir area might not
|
---|
| 309 | * need FAT entries if we decide to make a FAT32.
|
---|
[26ad20b] | 310 | */
|
---|
[602c3d8c] | 311 |
|
---|
| 312 | cfg->reserved_sectors = 1 + cfg->addt_res_sectors;
|
---|
[dccf721] | 313 |
|
---|
[a927398] | 314 | /* Only correct for FAT12/16 (FAT32 has root dir stored in clusters */
|
---|
| 315 | rd_sectors = div_round_up(cfg->root_ent_max * DIRENT_SIZE,
|
---|
[1433ecda] | 316 | cfg->sector_size);
|
---|
[a927398] | 317 | non_data_sectors_lb_16 = cfg->reserved_sectors + rd_sectors;
|
---|
| 318 |
|
---|
| 319 | /* Only correct for FAT12/16 */
|
---|
| 320 | tot_clust_16 = div_round_up(cfg->total_sectors - non_data_sectors_lb_16,
|
---|
[602c3d8c] | 321 | cfg->sectors_per_cluster);
|
---|
[dccf721] | 322 |
|
---|
[a927398] | 323 | /* Now detect FAT type */
|
---|
| 324 | if (tot_clust_16 <= FAT12_CLST_MAX) {
|
---|
[ed9bf14] | 325 | if (cfg->fat_type == FATAUTO)
|
---|
| 326 | cfg->fat_type = FAT12;
|
---|
| 327 | else if (cfg->fat_type != FAT12)
|
---|
| 328 | return EINVAL;
|
---|
[a927398] | 329 | } else if (tot_clust_16 <= FAT16_CLST_MAX) {
|
---|
[ed9bf14] | 330 | if (cfg->fat_type == FATAUTO)
|
---|
| 331 | cfg->fat_type = FAT16;
|
---|
| 332 | else if (cfg->fat_type != FAT16)
|
---|
| 333 | return EINVAL;
|
---|
| 334 | } else {
|
---|
| 335 | if (cfg->fat_type == FATAUTO)
|
---|
| 336 | cfg->fat_type = FAT32;
|
---|
| 337 | else if (cfg->fat_type != FAT32)
|
---|
| 338 | return EINVAL;
|
---|
| 339 | }
|
---|
[dccf721] | 340 |
|
---|
[a927398] | 341 | /* Actual root directory size, non-data sectors */
|
---|
| 342 | if (cfg->fat_type != FAT32) {
|
---|
| 343 | cfg->rootdir_sectors = div_round_up(cfg->root_ent_max * DIRENT_SIZE,
|
---|
[1433ecda] | 344 | cfg->sector_size);
|
---|
[a927398] | 345 | non_data_sectors_lb = cfg->reserved_sectors + cfg->rootdir_sectors;
|
---|
| 346 |
|
---|
| 347 | } else {
|
---|
| 348 | /* We create a single-cluster root dir */
|
---|
| 349 | cfg->rootdir_sectors = cfg->sectors_per_cluster;
|
---|
| 350 | non_data_sectors_lb = cfg->reserved_sectors;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | /* Actual total number of clusters */
|
---|
| 354 | cfg->total_clusters = div_round_up(cfg->total_sectors - non_data_sectors_lb,
|
---|
| 355 | cfg->sectors_per_cluster);
|
---|
| 356 |
|
---|
[51d0ee9] | 357 | fat_bytes = div_round_up((cfg->total_clusters + 2) *
|
---|
| 358 | FAT_CLUSTER_DOUBLE_SIZE(cfg->fat_type), 2);
|
---|
[602c3d8c] | 359 | cfg->fat_sectors = div_round_up(fat_bytes, cfg->sector_size);
|
---|
[dccf721] | 360 |
|
---|
[f4ae95a] | 361 | if (cfg->label != NULL && fat_label_encode(NULL, cfg->label) != EOK)
|
---|
| 362 | return EINVAL;
|
---|
| 363 |
|
---|
[dccf721] | 364 | return EOK;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | /** Create file system with the given parameters. */
|
---|
[b7fd2a0] | 368 | static errno_t fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id)
|
---|
[dccf721] | 369 | {
|
---|
[ed903174] | 370 | aoff64_t addr;
|
---|
[dccf721] | 371 | uint8_t *buffer;
|
---|
| 372 | int i;
|
---|
| 373 | uint32_t j;
|
---|
[b7fd2a0] | 374 | errno_t rc;
|
---|
[dccf721] | 375 | struct fat_bs bs;
|
---|
[f4ae95a] | 376 | fat_dentry_t *de;
|
---|
[dccf721] | 377 |
|
---|
[602c3d8c] | 378 | fat_bootsec_create(cfg, &bs);
|
---|
[dccf721] | 379 |
|
---|
[15f3c3f] | 380 | rc = block_write_direct(service_id, BS_BLOCK, 1, &bs);
|
---|
[dccf721] | 381 | if (rc != EOK)
|
---|
| 382 | return EIO;
|
---|
| 383 |
|
---|
| 384 | addr = BS_BLOCK + 1;
|
---|
| 385 |
|
---|
[602c3d8c] | 386 | buffer = calloc(cfg->sector_size, 1);
|
---|
[dccf721] | 387 | if (buffer == NULL)
|
---|
| 388 | return ENOMEM;
|
---|
[602c3d8c] | 389 | memset(buffer, 0, cfg->sector_size);
|
---|
[dccf721] | 390 |
|
---|
| 391 | /* Reserved sectors */
|
---|
[602c3d8c] | 392 | for (i = 0; i < cfg->reserved_sectors - 1; ++i) {
|
---|
[15f3c3f] | 393 | rc = block_write_direct(service_id, addr, 1, buffer);
|
---|
[dccf721] | 394 | if (rc != EOK)
|
---|
| 395 | return EIO;
|
---|
| 396 |
|
---|
| 397 | ++addr;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | /* File allocation tables */
|
---|
[602c3d8c] | 401 | for (i = 0; i < cfg->fat_count; ++i) {
|
---|
[dccf721] | 402 | printf("Writing allocation table %d.\n", i + 1);
|
---|
| 403 |
|
---|
[602c3d8c] | 404 | for (j = 0; j < cfg->fat_sectors; ++j) {
|
---|
| 405 | memset(buffer, 0, cfg->sector_size);
|
---|
[dccf721] | 406 | if (j == 0) {
|
---|
[602c3d8c] | 407 | buffer[0] = default_media_descriptor;
|
---|
[dccf721] | 408 | buffer[1] = 0xFF;
|
---|
| 409 | buffer[2] = 0xFF;
|
---|
[602c3d8c] | 410 | if (cfg->fat_type == FAT16) {
|
---|
| 411 | buffer[3] = 0xFF;
|
---|
| 412 | } else if (cfg->fat_type == FAT32) {
|
---|
| 413 | buffer[3] = 0x0F;
|
---|
| 414 | buffer[4] = 0xFF;
|
---|
| 415 | buffer[5] = 0xFF;
|
---|
| 416 | buffer[6] = 0xFF;
|
---|
| 417 | buffer[7] = 0x0F;
|
---|
| 418 | buffer[8] = 0xF8;
|
---|
| 419 | buffer[9] = 0xFF;
|
---|
| 420 | buffer[10] = 0xFF;
|
---|
| 421 | buffer[11] = 0x0F;
|
---|
| 422 | }
|
---|
[dccf721] | 423 | }
|
---|
| 424 |
|
---|
[15f3c3f] | 425 | rc = block_write_direct(service_id, addr, 1, buffer);
|
---|
[dccf721] | 426 | if (rc != EOK)
|
---|
| 427 | return EIO;
|
---|
| 428 |
|
---|
| 429 | ++addr;
|
---|
| 430 | }
|
---|
| 431 | }
|
---|
| 432 |
|
---|
[602c3d8c] | 433 | /* Root directory */
|
---|
[dccf721] | 434 | printf("Writing root directory.\n");
|
---|
[602c3d8c] | 435 | memset(buffer, 0, cfg->sector_size);
|
---|
[f4ae95a] | 436 | de = (fat_dentry_t *)buffer;
|
---|
| 437 | size_t idx;
|
---|
| 438 | for (idx = 0; idx < cfg->rootdir_sectors; ++idx) {
|
---|
| 439 |
|
---|
| 440 | if (idx == 0 && cfg->label != NULL) {
|
---|
| 441 | /* Set up volume label entry */
|
---|
| 442 | (void) fat_label_encode(&de->name, cfg->label);
|
---|
| 443 | de->attr = FAT_ATTR_VOLLABEL;
|
---|
| 444 | de->mtime = 0x1234;
|
---|
| 445 | de->mdate = 0x1234;
|
---|
| 446 | } else if (idx == 1) {
|
---|
| 447 | /* Clear volume label entry */
|
---|
| 448 | memset(buffer, 0, cfg->sector_size);
|
---|
[602c3d8c] | 449 | }
|
---|
[dccf721] | 450 |
|
---|
[f4ae95a] | 451 | rc = block_write_direct(service_id, addr, 1, buffer);
|
---|
| 452 | if (rc != EOK)
|
---|
| 453 | return EIO;
|
---|
| 454 |
|
---|
| 455 | ++addr;
|
---|
[dccf721] | 456 | }
|
---|
| 457 |
|
---|
| 458 | free(buffer);
|
---|
| 459 |
|
---|
| 460 | return EOK;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | /** Construct boot sector with the given parameters. */
|
---|
[602c3d8c] | 464 | static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs)
|
---|
[dccf721] | 465 | {
|
---|
[f4ae95a] | 466 | const char *bs_label;
|
---|
| 467 |
|
---|
| 468 | /*
|
---|
| 469 | * The boot sector must always contain a valid label. If there
|
---|
| 470 | * is no label, there should be 'NO NAME'
|
---|
| 471 | */
|
---|
| 472 | if (cfg->label != NULL)
|
---|
| 473 | bs_label = cfg->label;
|
---|
| 474 | else
|
---|
| 475 | bs_label = LABEL_NONAME;
|
---|
[dccf721] | 476 | memset(bs, 0, sizeof(*bs));
|
---|
| 477 |
|
---|
| 478 | bs->ji[0] = 0xEB;
|
---|
| 479 | bs->ji[1] = 0x3C;
|
---|
| 480 | bs->ji[2] = 0x90;
|
---|
| 481 |
|
---|
| 482 | memcpy(bs->oem_name, "HELENOS ", 8);
|
---|
| 483 |
|
---|
| 484 | /* BIOS Parameter Block */
|
---|
[602c3d8c] | 485 | bs->bps = host2uint16_t_le(cfg->sector_size);
|
---|
| 486 | bs->spc = cfg->sectors_per_cluster;
|
---|
| 487 | bs->rscnt = host2uint16_t_le(cfg->reserved_sectors);
|
---|
| 488 | bs->fatcnt = cfg->fat_count;
|
---|
| 489 | bs->root_ent_max = host2uint16_t_le(cfg->root_ent_max);
|
---|
| 490 |
|
---|
| 491 | if (cfg->total_sectors < 0x10000) {
|
---|
| 492 | bs->totsec16 = host2uint16_t_le(cfg->total_sectors);
|
---|
| 493 | bs->totsec32 = 0;
|
---|
| 494 | } else {
|
---|
| 495 | bs->totsec16 = 0;
|
---|
| 496 | bs->totsec32 = host2uint32_t_le(cfg->total_sectors);
|
---|
| 497 | }
|
---|
[dccf721] | 498 |
|
---|
[602c3d8c] | 499 | bs->mdesc = default_media_descriptor;
|
---|
[dccf721] | 500 | bs->sec_per_track = host2uint16_t_le(63);
|
---|
[602c3d8c] | 501 | bs->signature = host2uint16_t_be(0x55AA);
|
---|
[dccf721] | 502 | bs->headcnt = host2uint16_t_le(6);
|
---|
| 503 | bs->hidden_sec = host2uint32_t_le(0);
|
---|
| 504 |
|
---|
[602c3d8c] | 505 | if (cfg->fat_type == FAT32) {
|
---|
| 506 | bs->sec_per_fat = 0;
|
---|
| 507 | bs->fat32.sectors_per_fat = host2uint32_t_le(cfg->fat_sectors);
|
---|
[dccf721] | 508 |
|
---|
[602c3d8c] | 509 | bs->fat32.pdn = 0x80;
|
---|
| 510 | bs->fat32.ebs = 0x29;
|
---|
| 511 | bs->fat32.id = host2uint32_t_be(0x12345678);
|
---|
| 512 | bs->fat32.root_cluster = 2;
|
---|
[dccf721] | 513 |
|
---|
[f4ae95a] | 514 | (void) fat_label_encode(&bs->fat32.label, bs_label);
|
---|
[602c3d8c] | 515 | memcpy(bs->fat32.type, "FAT32 ", 8);
|
---|
| 516 | } else {
|
---|
| 517 | bs->sec_per_fat = host2uint16_t_le(cfg->fat_sectors);
|
---|
| 518 | bs->pdn = 0x80;
|
---|
| 519 | bs->ebs = 0x29;
|
---|
| 520 | bs->id = host2uint32_t_be(0x12345678);
|
---|
| 521 |
|
---|
[f4ae95a] | 522 | (void) fat_label_encode(&bs->label, bs_label);
|
---|
[58fa3e6] | 523 | if (cfg->fat_type == FAT12)
|
---|
| 524 | memcpy(bs->type, "FAT12 ", 8);
|
---|
| 525 | else
|
---|
| 526 | memcpy(bs->type, "FAT16 ", 8);
|
---|
[602c3d8c] | 527 | }
|
---|
[dccf721] | 528 | }
|
---|
| 529 |
|
---|
| 530 | /**
|
---|
| 531 | * @}
|
---|
| 532 | */
|
---|