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