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