Changeset 87337dc5 in mainline


Ignore:
Timestamp:
2018-07-09T15:00:24Z (6 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d054ad3
Parents:
914c693
git-author:
Jiri Svoboda <jiri@…> (2018-07-08 19:58:56)
git-committer:
Jiri Svoboda <jiri@…> (2018-07-09 15:00:24)
Message:

Random number generator interface. FAT and exFAT should be created with random volume serial numbers.

Location:
uspace
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/mkexfat/mkexfat.c

    r914c693 r87337dc5  
    4646#include <byteorder.h>
    4747#include <align.h>
     48#include <rndgen.h>
    4849#include <str.h>
    4950#include <getopt.h>
     
    243244 * @param mbs Pointer to the Main Boot Sector structure.
    244245 * @param cfg Pointer to the exFAT configuration structure.
    245  * @return    Initial checksum value.
     246 * @param chksum Place to store initial checksum value.
     247 * @return EOK on success or error code
    246248 */
    247249static uint32_t
    248 vbr_initialize(exfat_bs_t *mbs, exfat_cfg_t *cfg)
    249 {
     250vbr_initialize(exfat_bs_t *mbs, exfat_cfg_t *cfg, uint32_t *chksum)
     251{
     252        rndgen_t *rndgen;
     253        errno_t rc;
     254        uint32_t vsn;
     255
     256        /* Generate volume serial number */
     257
     258        rc = rndgen_create(&rndgen);
     259        if (rc != EOK)
     260                return rc;
     261
     262        rc = rndgen_uint32(rndgen, &vsn);
     263        if (rc != EOK) {
     264                rndgen_destroy(rndgen);
     265                return rc;
     266        }
     267
     268        rndgen_destroy(rndgen);
     269
    250270        /* Fill the structure with zeroes */
    251271        memset(mbs, 0, sizeof(exfat_bs_t));
     
    268288
    269289        mbs->rootdir_cluster = host2uint32_t_le(cfg->rootdir_cluster);
    270         mbs->volume_serial = host2uint32_t_le(0xe1028172);
     290        mbs->volume_serial = host2uint32_t_le(vsn);
    271291        mbs->version.major = 1;
    272292        mbs->version.minor = 0;
     
    283303        mbs->signature = host2uint16_t_le(0xAA55);
    284304
    285         return vbr_checksum_start(mbs, sizeof(exfat_bs_t));
     305        *chksum = vbr_checksum_start(mbs, sizeof(exfat_bs_t));
     306        return EOK;
    286307}
    287308
     
    299320                return ENOMEM;
    300321
    301         vbr_checksum = vbr_initialize(&mbs, cfg);
     322        rc = vbr_initialize(&mbs, cfg, &vbr_checksum);
     323        if (rc != EOK)
     324                goto exit;
    302325
    303326        /* Write the Main Boot Sector to disk */
  • uspace/app/mkfat/mkfat.c

    r914c693 r87337dc5  
    4848#include <inttypes.h>
    4949#include <errno.h>
     50#include <rndgen.h>
    5051#include <str.h>
    5152#include "fat.h"
     
    8990static errno_t fat_params_compute(struct fat_cfg *cfg);
    9091static errno_t fat_blocks_write(struct fat_cfg const *cfg, service_id_t service_id);
    91 static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs);
     92static errno_t fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs);
    9293
    9394int main(int argc, char **argv)
     
    376377        fat_dentry_t *de;
    377378
    378         fat_bootsec_create(cfg, &bs);
     379        rc = fat_bootsec_create(cfg, &bs);
     380        if (rc != EOK)
     381                return rc;
    379382
    380383        rc = block_write_direct(service_id, BS_BLOCK, 1, &bs);
     
    442445                        (void) fat_label_encode(&de->name, cfg->label);
    443446                        de->attr = FAT_ATTR_VOLLABEL;
    444                         de->mtime = 0x1234;
    445                         de->mdate = 0x1234;
     447                        de->mtime = 0x1234; // XXX Proper time
     448                        de->mdate = 0x1234; // XXX Proper date
    446449                } else if (idx == 1) {
    447450                        /* Clear volume label entry */
     
    462465
    463466/** Construct boot sector with the given parameters. */
    464 static void fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs)
     467static errno_t fat_bootsec_create(struct fat_cfg const *cfg, struct fat_bs *bs)
    465468{
    466469        const char *bs_label;
     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);
    467486
    468487        /*
     
    503522        bs->hidden_sec = host2uint32_t_le(0);
    504523
     524
    505525        if (cfg->fat_type == FAT32) {
    506526                bs->sec_per_fat = 0;
     
    509529                bs->fat32.pdn = 0x80;
    510530                bs->fat32.ebs = 0x29;
    511                 bs->fat32.id = host2uint32_t_be(0x12345678);
     531                bs->fat32.id = host2uint32_t_be(vsn);
    512532                bs->fat32.root_cluster = 2;
    513533
     
    518538                bs->pdn = 0x80;
    519539                bs->ebs = 0x29;
    520                 bs->id = host2uint32_t_be(0x12345678);
     540                bs->id = host2uint32_t_be(vsn);
    521541
    522542                (void) fat_label_encode(&bs->label, bs_label);
     
    526546                        memcpy(bs->type, "FAT16   ", 8);
    527547        }
     548
     549        return EOK;
    528550}
    529551
  • uspace/lib/c/Makefile

    r914c693 r87337dc5  
    133133        generic/double_to_str.c \
    134134        generic/malloc.c \
     135        generic/rndgen.c \
    135136        generic/stdio/scanf.c \
    136137        generic/stdio/sprintf.c \
  • uspace/lib/c/generic/uuid.c

    r914c693 r87337dc5  
    3535#include <errno.h>
    3636#include <uuid.h>
     37#include <rndgen.h>
    3738#include <stdlib.h>
    3839#include <stddef.h>
    39 #include <time.h>
    4040#include <str.h>
    4141
     
    4848{
    4949        int i;
    50         struct timeval tv;
     50        rndgen_t *rndgen;
     51        errno_t rc;
    5152
    52         /* XXX This is a rather poor way of generating random numbers */
    53         gettimeofday(&tv, NULL);
    54         srand(tv.tv_sec ^ tv.tv_usec);
     53        rc = rndgen_create(&rndgen);
     54        if (rc != EOK)
     55                return EIO;
    5556
    56         for (i = 0; i < uuid_bytes; i++)
    57                 uuid->b[i] = rand();
     57        for (i = 0; i < uuid_bytes; i++) {
     58                rc = rndgen_uint8(rndgen, &uuid->b[i]);
     59                if (rc != EOK) {
     60                        rc = EIO;
     61                        goto error;
     62                }
     63        }
    5864
    5965        /* Version 4 UUID from random or pseudo-random numbers */
     
    6268
    6369        return EOK;
     70error:
     71        rndgen_destroy(rndgen);
     72        return rc;
    6473}
    6574
Note: See TracChangeset for help on using the changeset viewer.