| 1 | /*
|
|---|
| 2 | * Copyright (c) 2024 Miroslav Cimerman
|
|---|
| 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 bdwrite
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <block.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <getopt.h>
|
|---|
| 39 | #include <mem.h>
|
|---|
| 40 | #include <stdlib.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <abi/ipc/ipc.h>
|
|---|
| 43 |
|
|---|
| 44 | static void usage(void);
|
|---|
| 45 |
|
|---|
| 46 | static const char usage_str[] =
|
|---|
| 47 | "Usage: bdwrite <dev> -o <offset in blocks> -c <block count>\n"
|
|---|
| 48 | "\n"
|
|---|
| 49 | " Write cyclic blocks to block device.\n";
|
|---|
| 50 |
|
|---|
| 51 | static struct option const long_options[] = {
|
|---|
| 52 | { 0, 0, 0, 0 }
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | static void usage(void)
|
|---|
| 56 | {
|
|---|
| 57 | printf("%s", usage_str);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | int main(int argc, char **argv)
|
|---|
| 61 | {
|
|---|
| 62 | errno_t rc;
|
|---|
| 63 | size_t bsize;
|
|---|
| 64 | int c;
|
|---|
| 65 | char *name = NULL;
|
|---|
| 66 | size_t blkcnt = 0, off = 0;
|
|---|
| 67 | service_id_t dev;
|
|---|
| 68 |
|
|---|
| 69 | if (argc != 6) {
|
|---|
| 70 | goto bad;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | name = argv[1];
|
|---|
| 74 |
|
|---|
| 75 | c = 0;
|
|---|
| 76 | optreset = 1;
|
|---|
| 77 | optind = 0;
|
|---|
| 78 |
|
|---|
| 79 | while (c != -1) {
|
|---|
| 80 | c = getopt_long(argc, argv, "o:c:", long_options, NULL);
|
|---|
| 81 | switch (c) {
|
|---|
| 82 | case 'o':
|
|---|
| 83 | off = strtol(optarg, NULL, 10);
|
|---|
| 84 | break;
|
|---|
| 85 | case 'c':
|
|---|
| 86 | blkcnt = strtol(optarg, NULL, 10);
|
|---|
| 87 | break;
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | rc = loc_service_get_id(name, &dev, 0);
|
|---|
| 92 | if (rc != EOK) {
|
|---|
| 93 | printf("bdwrite: error resolving device \"%s\"\n", name);
|
|---|
| 94 | return 1;
|
|---|
| 95 | }
|
|---|
| 96 | rc = block_init(dev);
|
|---|
| 97 | if (rc != EOK) {
|
|---|
| 98 | printf("bdwrite: error initializing block device \"%s\"\n", name);
|
|---|
| 99 | return 1;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | rc = block_get_bsize(dev, &bsize);
|
|---|
| 103 | if (rc != EOK) {
|
|---|
| 104 | printf("bdwrite: error getting block size of \"%s\"\n", name);
|
|---|
| 105 | block_fini(dev);
|
|---|
| 106 | return 1;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | uint64_t to_alloc = min(DATA_XFER_LIMIT, bsize * blkcnt);
|
|---|
| 110 | uint8_t *buf = malloc(to_alloc);
|
|---|
| 111 | if (buf == NULL) {
|
|---|
| 112 | rc = ENOMEM;
|
|---|
| 113 | goto end;
|
|---|
| 114 | }
|
|---|
| 115 | uint64_t left = blkcnt;
|
|---|
| 116 | while (left != 0) {
|
|---|
| 117 | uint64_t blks_to_write = min(to_alloc / bsize, left);
|
|---|
| 118 | uint8_t *ptr = buf;
|
|---|
| 119 | for (size_t i = 0; i < blks_to_write; i++) {
|
|---|
| 120 | /* memset(ptr, (i + 1) % 0x100, bsize); */
|
|---|
| 121 | memset(ptr, 'A' + (i % 26), bsize);
|
|---|
| 122 | ptr += bsize;
|
|---|
| 123 | }
|
|---|
| 124 | rc = block_write_direct(dev, off, blks_to_write, buf);
|
|---|
| 125 | if (rc != EOK) {
|
|---|
| 126 | printf("bdwrite: error writing to \"%s\"\n", name);
|
|---|
| 127 | goto end;
|
|---|
| 128 | }
|
|---|
| 129 | left -= blks_to_write;
|
|---|
| 130 | off += blks_to_write;
|
|---|
| 131 | }
|
|---|
| 132 | end:
|
|---|
| 133 | free(buf);
|
|---|
| 134 | block_fini(dev);
|
|---|
| 135 | return rc;
|
|---|
| 136 | bad:
|
|---|
| 137 | usage();
|
|---|
| 138 | return 0;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /** @}
|
|---|
| 142 | */
|
|---|