Index: head/bin/dd/args.c =================================================================== --- head/bin/dd/args.c (revision 351769) +++ head/bin/dd/args.c (revision 351770) @@ -1,513 +1,514 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1991, 1993, 1994 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Keith Muller of the University of California, San Diego and Lance * Visser of Convex Computer Corporation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint #if 0 static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94"; #endif #endif /* not lint */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include "dd.h" #include "extern.h" static int c_arg(const void *, const void *); static int c_conv(const void *, const void *); static void f_bs(char *); static void f_cbs(char *); static void f_conv(char *); static void f_count(char *); static void f_files(char *); static void f_fillchar(char *); static void f_ibs(char *); static void f_if(char *); static void f_obs(char *); static void f_of(char *); static void f_seek(char *); static void f_skip(char *); static void f_speed(char *); static void f_status(char *); static uintmax_t get_num(const char *); static off_t get_off_t(const char *); static const struct arg { const char *name; void (*f)(char *); u_int set, noset; } args[] = { { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC }, { "cbs", f_cbs, C_CBS, C_CBS }, { "conv", f_conv, 0, 0 }, { "count", f_count, C_COUNT, C_COUNT }, { "files", f_files, C_FILES, C_FILES }, { "fillchar", f_fillchar, C_FILL, C_FILL }, { "ibs", f_ibs, C_IBS, C_BS|C_IBS }, { "if", f_if, C_IF, C_IF }, { "iseek", f_skip, C_SKIP, C_SKIP }, { "obs", f_obs, C_OBS, C_BS|C_OBS }, { "of", f_of, C_OF, C_OF }, { "oseek", f_seek, C_SEEK, C_SEEK }, { "seek", f_seek, C_SEEK, C_SEEK }, { "skip", f_skip, C_SKIP, C_SKIP }, { "speed", f_speed, 0, 0 }, { "status", f_status, C_STATUS,C_STATUS }, }; static char *oper; /* * args -- parse JCL syntax of dd. */ void jcl(char **argv) { struct arg *ap, tmp; char *arg; in.dbsz = out.dbsz = 512; while ((oper = *++argv) != NULL) { if ((oper = strdup(oper)) == NULL) errx(1, "unable to allocate space for the argument \"%s\"", *argv); if ((arg = strchr(oper, '=')) == NULL) errx(1, "unknown operand %s", oper); *arg++ = '\0'; if (!*arg) errx(1, "no value specified for %s", oper); tmp.name = oper; if (!(ap = (struct arg *)bsearch(&tmp, args, sizeof(args)/sizeof(struct arg), sizeof(struct arg), c_arg))) errx(1, "unknown operand %s", tmp.name); if (ddflags & ap->noset) errx(1, "%s: illegal argument combination or already set", tmp.name); ddflags |= ap->set; ap->f(arg); } /* Final sanity checks. */ if (ddflags & C_BS) { /* * Bs is turned off by any conversion -- we assume the user * just wanted to set both the input and output block sizes * and didn't want the bs semantics, so we don't warn. */ if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE | C_UNBLOCK)) ddflags &= ~C_BS; /* Bs supersedes ibs and obs. */ if (ddflags & C_BS && ddflags & (C_IBS | C_OBS)) warnx("bs supersedes ibs and obs"); } /* * Ascii/ebcdic and cbs implies block/unblock. * Block/unblock requires cbs and vice-versa. */ if (ddflags & (C_BLOCK | C_UNBLOCK)) { if (!(ddflags & C_CBS)) errx(1, "record operations require cbs"); if (cbsz == 0) errx(1, "cbs cannot be zero"); cfunc = ddflags & C_BLOCK ? block : unblock; } else if (ddflags & C_CBS) { if (ddflags & (C_ASCII | C_EBCDIC)) { if (ddflags & C_ASCII) { ddflags |= C_UNBLOCK; cfunc = unblock; } else { ddflags |= C_BLOCK; cfunc = block; } } else errx(1, "cbs meaningless if not doing record operations"); } else cfunc = def; } static int c_arg(const void *a, const void *b) { return (strcmp(((const struct arg *)a)->name, ((const struct arg *)b)->name)); } static void f_bs(char *arg) { uintmax_t res; res = get_num(arg); if (res < 1 || res > SSIZE_MAX) errx(1, "bs must be between 1 and %zd", (ssize_t)SSIZE_MAX); in.dbsz = out.dbsz = (size_t)res; } static void f_cbs(char *arg) { uintmax_t res; res = get_num(arg); if (res < 1 || res > SSIZE_MAX) errx(1, "cbs must be between 1 and %zd", (ssize_t)SSIZE_MAX); cbsz = (size_t)res; } static void f_count(char *arg) { uintmax_t res; res = get_num(arg); if (res == UINTMAX_MAX) errc(1, ERANGE, "%s", oper); if (res == 0) cpy_cnt = UINTMAX_MAX; else cpy_cnt = res; } static void f_files(char *arg) { files_cnt = get_num(arg); if (files_cnt < 1) errx(1, "files must be between 1 and %zu", SIZE_MAX); } static void f_fillchar(char *arg) { if (strlen(arg) != 1) errx(1, "need exactly one fill char"); fill_char = arg[0]; } static void f_ibs(char *arg) { uintmax_t res; if (!(ddflags & C_BS)) { res = get_num(arg); if (res < 1 || res > SSIZE_MAX) errx(1, "ibs must be between 1 and %zd", (ssize_t)SSIZE_MAX); in.dbsz = (size_t)res; } } static void f_if(char *arg) { in.name = arg; } static void f_obs(char *arg) { uintmax_t res; if (!(ddflags & C_BS)) { res = get_num(arg); if (res < 1 || res > SSIZE_MAX) errx(1, "obs must be between 1 and %zd", (ssize_t)SSIZE_MAX); out.dbsz = (size_t)res; } } static void f_of(char *arg) { out.name = arg; } static void f_seek(char *arg) { out.offset = get_off_t(arg); } static void f_skip(char *arg) { in.offset = get_off_t(arg); } static void f_speed(char *arg) { speed = get_num(arg); } static void f_status(char *arg) { if (strcmp(arg, "none") == 0) ddflags |= C_NOINFO; else if (strcmp(arg, "noxfer") == 0) ddflags |= C_NOXFER; else if (strcmp(arg, "progress") == 0) ddflags |= C_PROGRESS; else errx(1, "unknown status %s", arg); } static const struct conv { const char *name; u_int set, noset; const u_char *ctab; } clist[] = { { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX }, { "block", C_BLOCK, C_UNBLOCK, NULL }, { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX }, + { "fsync", C_FSYNC, 0, NULL }, { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX }, { "lcase", C_LCASE, C_UCASE, NULL }, { "noerror", C_NOERROR, 0, NULL }, { "notrunc", C_NOTRUNC, 0, NULL }, { "oldascii", C_ASCII, C_EBCDIC, e2a_32V }, { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V }, { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V }, { "osync", C_OSYNC, C_BS, NULL }, { "pareven", C_PAREVEN, C_PARODD|C_PARSET|C_PARNONE, NULL}, { "parnone", C_PARNONE, C_PARODD|C_PARSET|C_PAREVEN, NULL}, { "parodd", C_PARODD, C_PAREVEN|C_PARSET|C_PARNONE, NULL}, { "parset", C_PARSET, C_PARODD|C_PAREVEN|C_PARNONE, NULL}, { "sparse", C_SPARSE, 0, NULL }, { "swab", C_SWAB, 0, NULL }, { "sync", C_SYNC, 0, NULL }, { "ucase", C_UCASE, C_LCASE, NULL }, { "unblock", C_UNBLOCK, C_BLOCK, NULL }, }; static void f_conv(char *arg) { struct conv *cp, tmp; while (arg != NULL) { tmp.name = strsep(&arg, ","); cp = bsearch(&tmp, clist, sizeof(clist) / sizeof(struct conv), sizeof(struct conv), c_conv); if (cp == NULL) errx(1, "unknown conversion %s", tmp.name); if (ddflags & cp->noset) errx(1, "%s: illegal conversion combination", tmp.name); ddflags |= cp->set; if (cp->ctab) ctab = cp->ctab; } } static int c_conv(const void *a, const void *b) { return (strcmp(((const struct conv *)a)->name, ((const struct conv *)b)->name)); } static intmax_t postfix_to_mult(const char expr) { intmax_t mult; mult = 0; switch (expr) { case 'B': case 'b': mult = 512; break; case 'K': case 'k': mult = 1 << 10; break; case 'M': case 'm': mult = 1 << 20; break; case 'G': case 'g': mult = 1 << 30; break; case 'T': case 't': mult = (uintmax_t)1 << 40; break; case 'P': case 'p': mult = (uintmax_t)1 << 50; break; case 'W': case 'w': mult = sizeof(int); break; } return (mult); } /* * Convert an expression of the following forms to a uintmax_t. * 1) A positive decimal number. * 2) A positive decimal number followed by a 'b' or 'B' (mult by 512). * 3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10). * 4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20). * 5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30). * 6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40). * 7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50). * 8) A positive decimal number followed by a 'w' or 'W' (mult by sizeof int). * 9) Two or more positive decimal numbers (with/without [BbKkMmGgWw]) * separated by 'x' or 'X' (also '*' for backwards compatibility), * specifying the product of the indicated values. */ static uintmax_t get_num(const char *val) { uintmax_t num, mult, prevnum; char *expr; errno = 0; num = strtoumax(val, &expr, 0); if (expr == val) /* No valid digits. */ errx(1, "%s: invalid numeric value", oper); if (errno != 0) err(1, "%s", oper); mult = postfix_to_mult(*expr); if (mult != 0) { prevnum = num; num *= mult; /* Check for overflow. */ if (num / mult != prevnum) goto erange; expr++; } switch (*expr) { case '\0': break; case '*': /* Backward compatible. */ case 'X': case 'x': mult = get_num(expr + 1); prevnum = num; num *= mult; if (num / mult == prevnum) break; erange: errx(1, "%s: %s", oper, strerror(ERANGE)); default: errx(1, "%s: illegal numeric value", oper); } return (num); } /* * Convert an expression of the following forms to an off_t. This is the * same as get_num(), but it uses signed numbers. * * The major problem here is that an off_t may not necessarily be a intmax_t. */ static off_t get_off_t(const char *val) { intmax_t num, mult, prevnum; char *expr; errno = 0; num = strtoimax(val, &expr, 0); if (expr == val) /* No valid digits. */ errx(1, "%s: invalid numeric value", oper); if (errno != 0) err(1, "%s", oper); mult = postfix_to_mult(*expr); if (mult != 0) { prevnum = num; num *= mult; /* Check for overflow. */ if ((prevnum > 0) != (num > 0) || num / mult != prevnum) goto erange; expr++; } switch (*expr) { case '\0': break; case '*': /* Backward compatible. */ case 'X': case 'x': mult = (intmax_t)get_off_t(expr + 1); prevnum = num; num *= mult; if ((prevnum > 0) == (num > 0) && num / mult == prevnum) break; erange: errx(1, "%s: %s", oper, strerror(ERANGE)); default: errx(1, "%s: illegal numeric value", oper); } return (num); } Index: head/bin/dd/dd.1 =================================================================== --- head/bin/dd/dd.1 (revision 351769) +++ head/bin/dd/dd.1 (revision 351770) @@ -1,465 +1,469 @@ .\"- .\" Copyright (c) 1990, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" Keith Muller of the University of California, San Diego. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)dd.1 8.2 (Berkeley) 1/13/94 .\" $FreeBSD$ .\" .Dd March 26, 2019 .Dt DD 1 .Os .Sh NAME .Nm dd .Nd convert and copy a file .Sh SYNOPSIS .Nm .Op Ar operands ... .Sh DESCRIPTION The .Nm utility copies the standard input to the standard output. Input data is read and written in 512-byte blocks. If input reads are short, input from multiple reads are aggregated to form the output block. When finished, .Nm displays the number of complete and partial input and output blocks and truncated input records to the standard error output. .Pp The following operands are available: .Bl -tag -width "of=file" .It Cm bs Ns = Ns Ar n Set both input and output block size to .Ar n bytes, superseding the .Cm ibs and .Cm obs operands. If no conversion values other than .Cm noerror , .Cm notrunc or .Cm sync are specified, then each input block is copied to the output as a single block without any aggregation of short blocks. .It Cm cbs Ns = Ns Ar n Set the conversion record size to .Ar n bytes. The conversion record size is required by the record oriented conversion values. .It Cm count Ns = Ns Ar n Copy only .Ar n input blocks. .It Cm files Ns = Ns Ar n Copy .Ar n input files before terminating. This operand is only applicable when the input device is a tape. .It Cm fillchar Ns = Ns Ar c When padding a block in conversion mode or due to use of .Cm noerror and .Cm sync modes, fill with the specified .Tn ASCII character, rather than using a space or .Dv NUL . .It Cm ibs Ns = Ns Ar n Set the input block size to .Ar n bytes instead of the default 512. .It Cm if Ns = Ns Ar file Read input from .Ar file instead of the standard input. .It Cm iseek Ns = Ns Ar n Seek on the input file .Ar n blocks. This is synonymous with .Cm skip Ns = Ns Ar n . .It Cm obs Ns = Ns Ar n Set the output block size to .Ar n bytes instead of the default 512. .It Cm of Ns = Ns Ar file Write output to .Ar file instead of the standard output. Any regular output file is truncated unless the .Cm notrunc conversion value is specified. If an initial portion of the output file is seeked past (see the .Cm oseek operand), the output file is truncated at that point. .It Cm oseek Ns = Ns Ar n Seek on the output file .Ar n blocks. This is synonymous with .Cm seek Ns = Ns Ar n . .It Cm seek Ns = Ns Ar n Seek .Ar n blocks from the beginning of the output before copying. On non-tape devices, an .Xr lseek 2 operation is used. Otherwise, existing blocks are read and the data discarded. If the user does not have read permission for the tape, it is positioned using the tape .Xr ioctl 2 function calls. If the seek operation is past the end of file, space from the current end of file to the specified offset is filled with blocks of .Dv NUL bytes. .It Cm skip Ns = Ns Ar n Skip .Ar n blocks from the beginning of the input before copying. On input which supports seeks, an .Xr lseek 2 operation is used. Otherwise, input data is read and discarded. For pipes, the correct number of bytes is read. For all other devices, the correct number of blocks is read without distinguishing between a partial or complete block being read. .It Cm speed Ns = Ns Ar n Limit the copying speed to .Ar n bytes per second. .It Cm status Ns = Ns Ar value Where .Cm value is one of the symbols from the following list. .Bl -tag -width "progress" .It Cm noxfer Do not print the transfer statistics as the last line of status output. .It Cm none Do not print the status output. Error messages are shown; informational messages are not. .It Cm progress Print basic transfer statistics once per second. .El .It Cm conv Ns = Ns Ar value Ns Op , Ns Ar value ... Where .Cm value is one of the symbols from the following list. .Bl -tag -width "unblock" .It Cm ascii , oldascii The same as the .Cm unblock value except that characters are translated from .Tn EBCDIC to .Tn ASCII before the records are converted. (These values imply .Cm unblock if the operand .Cm cbs is also specified.) There are two conversion maps for .Tn ASCII . The value .Cm ascii specifies the recommended one which is compatible with .At V . The value .Cm oldascii specifies the one used in historic .At and .No pre- Ns Bx 4.3 reno systems. .It Cm block Treats the input as a sequence of newline or end-of-file terminated variable length records independent of input and output block boundaries. Any trailing newline character is discarded. Each input record is converted to a fixed length output record where the length is specified by the .Cm cbs operand. Input records shorter than the conversion record size are padded with spaces. Input records longer than the conversion record size are truncated. The number of truncated input records, if any, are reported to the standard error output at the completion of the copy. .It Cm ebcdic , ibm , oldebcdic , oldibm The same as the .Cm block value except that characters are translated from .Tn ASCII to .Tn EBCDIC after the records are converted. (These values imply .Cm block if the operand .Cm cbs is also specified.) There are four conversion maps for .Tn EBCDIC . The value .Cm ebcdic specifies the recommended one which is compatible with .At V . The value .Cm ibm is a slightly different mapping, which is compatible with the .At V .Cm ibm value. The values .Cm oldebcdic and .Cm oldibm are maps used in historic .At and .No pre- Ns Bx 4.3 reno systems. +.It Cm fsync +Perform an +.Xr fsync 2 +on the output file before closing it. .It Cm lcase Transform uppercase characters into lowercase characters. .It Cm pareven , parnone , parodd , parset Output data with the specified parity. The parity bit on input is stripped unless .Tn EBCDIC to .Tn ASCII conversions is also specified. .It Cm noerror Do not stop processing on an input error. When an input error occurs, a diagnostic message followed by the current input and output block counts will be written to the standard error output in the same format as the standard completion message. If the .Cm sync conversion is also specified, any missing input data will be replaced with .Dv NUL bytes (or with spaces if a block oriented conversion value was specified) and processed as a normal input buffer. If the .Cm fillchar option is specified, the fill character provided on the command line will override the automatic selection of the fill character. If the .Cm sync conversion is not specified, the input block is omitted from the output. On input files which are not tapes or pipes, the file offset will be positioned past the block in which the error occurred using .Xr lseek 2 . .It Cm notrunc Do not truncate the output file. This will preserve any blocks in the output file not explicitly written by .Nm . The .Cm notrunc value is not supported for tapes. .It Cm osync Pad the final output block to the full output block size. If the input file is not a multiple of the output block size after conversion, this conversion forces the final output block to be the same size as preceding blocks for use on devices that require regularly sized blocks to be written. This option is incompatible with use of the .Cm bs Ns = Ns Ar n block size specification. .It Cm sparse If one or more output blocks would consist solely of .Dv NUL bytes, try to seek the output file by the required space instead of filling them with .Dv NUL Ns s , resulting in a sparse file. .It Cm swab Swap every pair of input bytes. If an input buffer has an odd number of bytes, the last byte will be ignored during swapping. .It Cm sync Pad every input block to the input buffer size. Spaces are used for pad bytes if a block oriented conversion value is specified, otherwise .Dv NUL bytes are used. .It Cm ucase Transform lowercase characters into uppercase characters. .It Cm unblock Treats the input as a sequence of fixed length records independent of input and output block boundaries. The length of the input records is specified by the .Cm cbs operand. Any trailing space characters are discarded and a newline character is appended. .El .El .Pp Where sizes or speed are specified, a decimal, octal, or hexadecimal number of bytes is expected. If the number ends with a .Dq Li b , .Dq Li k , .Dq Li m , .Dq Li g , .Dq Li t , .Dq Li p , or .Dq Li w , the number is multiplied by 512, 1024 (1K), 1048576 (1M), 1073741824 (1G), 1099511627776 (1T), 1125899906842624 (1P) or the number of bytes in an integer, respectively. Two or more numbers may be separated by an .Dq Li x to indicate a product. .Pp When finished, .Nm displays the number of complete and partial input and output blocks, truncated input records and odd-length byte-swapping blocks to the standard error output. A partial input block is one where less than the input block size was read. A partial output block is one where less than the output block size was written. Partial output blocks to tape devices are considered fatal errors. Otherwise, the rest of the block will be written. Partial output blocks to character devices will produce a warning message. A truncated input block is one where a variable length record oriented conversion value was specified and the input line was too long to fit in the conversion record or was not newline terminated. .Pp Normally, data resulting from input or conversion or both are aggregated into output blocks of the specified size. After the end of input is reached, any remaining output is written as a block. This means that the final output block may be shorter than the output block size. .Pp If .Nm receives a .Dv SIGINFO (see the .Cm status argument for .Xr stty 1 ) signal, the current input and output block counts will be written to the standard error output in the same format as the standard completion message. If .Nm receives a .Dv SIGINT signal, the current input and output block counts will be written to the standard error output in the same format as the standard completion message and .Nm will exit. .Sh EXIT STATUS .Ex -std .Sh EXAMPLES Check that a disk drive contains no bad blocks: .Pp .Dl "dd if=/dev/ada0 of=/dev/null bs=1m" .Pp Do a refresh of a disk drive, in order to prevent presently recoverable read errors from progressing into unrecoverable read errors: .Pp .Dl "dd if=/dev/ada0 of=/dev/ada0 bs=1m" .Pp Remove parity bit from a file: .Pp .Dl "dd if=file conv=parnone of=file.txt" .Pp Check for (even) parity errors on a file: .Pp .Dl "dd if=file conv=pareven | cmp -x - file" .Pp To create an image of a Mode-1 CD-ROM, which is a commonly used format for data CD-ROM disks, use a block size of 2048 bytes: .Pp .Dl "dd if=/dev/cd0 of=filename.iso bs=2048" .Pp Write a filesystem image to a memory stick, padding the end with zeros, if necessary, to a 1MiB boundary: .Pp .Dl "dd if=memstick.img of=/dev/da0 bs=1m conv=noerror,sync" .Sh SEE ALSO .Xr cp 1 , .Xr mt 1 , .Xr recoverdisk 1 , .Xr tr 1 , .Xr geom 4 , .Xr trim 8 .Sh STANDARDS The .Nm utility is expected to be a superset of the .St -p1003.2 standard. The .Cm files and .Cm status operands and the .Cm ascii , .Cm ebcdic , .Cm ibm , .Cm oldascii , .Cm oldebcdic and .Cm oldibm values are extensions to the .Tn POSIX standard. .Sh HISTORY A .Nm command appeared in .At v5 . .Sh BUGS Protection mechanisms in the .Xr geom 4 subsystem might prevent the super-user from writing blocks to a disk. Instructions for temporarily disabling these protection mechanisms can be found in the .Xr geom 4 man page. Index: head/bin/dd/dd.c =================================================================== --- head/bin/dd/dd.c (revision 351769) +++ head/bin/dd/dd.c (revision 351770) @@ -1,613 +1,620 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1991, 1993, 1994 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Keith Muller of the University of California, San Diego and Lance * Visser of Convex Computer Corporation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if 0 #ifndef lint static char const copyright[] = "@(#) Copyright (c) 1991, 1993, 1994\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94"; #endif /* not lint */ #endif #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "dd.h" #include "extern.h" static void dd_close(void); static void dd_in(void); static void getfdtype(IO *); static void setup(void); IO in, out; /* input/output state */ STAT st; /* statistics */ void (*cfunc)(void); /* conversion function */ uintmax_t cpy_cnt; /* # of blocks to copy */ static off_t pending = 0; /* pending seek if sparse */ u_int ddflags = 0; /* conversion options */ size_t cbsz; /* conversion block size */ uintmax_t files_cnt = 1; /* # of files to copy */ const u_char *ctab; /* conversion table */ char fill_char; /* Character to fill with if defined */ size_t speed = 0; /* maximum speed, in bytes per second */ volatile sig_atomic_t need_summary; volatile sig_atomic_t need_progress; int main(int argc __unused, char *argv[]) { struct itimerval itv = { { 1, 0 }, { 1, 0 } }; /* SIGALARM every second, if needed */ (void)setlocale(LC_CTYPE, ""); jcl(argv); setup(); caph_cache_catpages(); if (caph_enter() < 0) err(1, "unable to enter capability mode"); (void)signal(SIGINFO, siginfo_handler); if (ddflags & C_PROGRESS) { (void)signal(SIGALRM, sigalarm_handler); setitimer(ITIMER_REAL, &itv, NULL); } (void)signal(SIGINT, terminate); atexit(summary); while (files_cnt--) dd_in(); dd_close(); /* * Some devices such as cfi(4) may perform significant amounts * of work when a write descriptor is closed. Close the out * descriptor explicitly so that the summary handler (called * from an atexit() hook) includes this work. */ close(out.fd); exit(0); } static int parity(u_char c) { int i; i = c ^ (c >> 1) ^ (c >> 2) ^ (c >> 3) ^ (c >> 4) ^ (c >> 5) ^ (c >> 6) ^ (c >> 7); return (i & 1); } static void setup(void) { u_int cnt; cap_rights_t rights; unsigned long cmds[] = { FIODTYPE, MTIOCTOP }; if (in.name == NULL) { in.name = "stdin"; in.fd = STDIN_FILENO; } else { in.fd = open(in.name, O_RDONLY, 0); if (in.fd == -1) err(1, "%s", in.name); } getfdtype(&in); cap_rights_init(&rights, CAP_READ, CAP_SEEK); if (caph_rights_limit(in.fd, &rights) == -1) err(1, "unable to limit capability rights"); if (files_cnt > 1 && !(in.flags & ISTAPE)) errx(1, "files is not supported for non-tape devices"); cap_rights_set(&rights, CAP_FTRUNCATE, CAP_IOCTL, CAP_WRITE); + if (ddflags & C_FSYNC) + cap_rights_set(&rights, CAP_FSYNC); if (out.name == NULL) { /* No way to check for read access here. */ out.fd = STDOUT_FILENO; out.name = "stdout"; } else { #define OFLAGS \ (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC)) out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE); /* * May not have read access, so try again with write only. * Without read we may have a problem if output also does * not support seeks. */ if (out.fd == -1) { out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE); out.flags |= NOREAD; cap_rights_clear(&rights, CAP_READ); } if (out.fd == -1) err(1, "%s", out.name); } getfdtype(&out); if (caph_rights_limit(out.fd, &rights) == -1) err(1, "unable to limit capability rights"); if (caph_ioctls_limit(out.fd, cmds, nitems(cmds)) == -1) err(1, "unable to limit capability rights"); if (in.fd != STDIN_FILENO && out.fd != STDIN_FILENO) { if (caph_limit_stdin() == -1) err(1, "unable to limit capability rights"); } if (in.fd != STDOUT_FILENO && out.fd != STDOUT_FILENO) { if (caph_limit_stdout() == -1) err(1, "unable to limit capability rights"); } if (in.fd != STDERR_FILENO && out.fd != STDERR_FILENO) { if (caph_limit_stderr() == -1) err(1, "unable to limit capability rights"); } /* * Allocate space for the input and output buffers. If not doing * record oriented I/O, only need a single buffer. */ if (!(ddflags & (C_BLOCK | C_UNBLOCK))) { if ((in.db = malloc((size_t)out.dbsz + in.dbsz - 1)) == NULL) err(1, "input buffer"); out.db = in.db; } else if ((in.db = malloc(MAX((size_t)in.dbsz, cbsz) + cbsz)) == NULL || (out.db = malloc(out.dbsz + cbsz)) == NULL) err(1, "output buffer"); /* dbp is the first free position in each buffer. */ in.dbp = in.db; out.dbp = out.db; /* Position the input/output streams. */ if (in.offset) pos_in(); if (out.offset) pos_out(); /* * Truncate the output file. If it fails on a type of output file * that it should _not_ fail on, error out. */ if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) && out.flags & ISTRUNC) if (ftruncate(out.fd, out.offset * out.dbsz) == -1) err(1, "truncating %s", out.name); if (ddflags & (C_LCASE | C_UCASE | C_ASCII | C_EBCDIC | C_PARITY)) { if (ctab != NULL) { for (cnt = 0; cnt <= 0377; ++cnt) casetab[cnt] = ctab[cnt]; } else { for (cnt = 0; cnt <= 0377; ++cnt) casetab[cnt] = cnt; } if ((ddflags & C_PARITY) && !(ddflags & C_ASCII)) { /* * If the input is not EBCDIC, and we do parity * processing, strip input parity. */ for (cnt = 200; cnt <= 0377; ++cnt) casetab[cnt] = casetab[cnt & 0x7f]; } if (ddflags & C_LCASE) { for (cnt = 0; cnt <= 0377; ++cnt) casetab[cnt] = tolower(casetab[cnt]); } else if (ddflags & C_UCASE) { for (cnt = 0; cnt <= 0377; ++cnt) casetab[cnt] = toupper(casetab[cnt]); } if ((ddflags & C_PARITY)) { /* * This should strictly speaking be a no-op, but I * wonder what funny LANG settings could get us. */ for (cnt = 0; cnt <= 0377; ++cnt) casetab[cnt] = casetab[cnt] & 0x7f; } if ((ddflags & C_PARSET)) { for (cnt = 0; cnt <= 0377; ++cnt) casetab[cnt] = casetab[cnt] | 0x80; } if ((ddflags & C_PAREVEN)) { for (cnt = 0; cnt <= 0377; ++cnt) if (parity(casetab[cnt])) casetab[cnt] = casetab[cnt] | 0x80; } if ((ddflags & C_PARODD)) { for (cnt = 0; cnt <= 0377; ++cnt) if (!parity(casetab[cnt])) casetab[cnt] = casetab[cnt] | 0x80; } ctab = casetab; } if (clock_gettime(CLOCK_MONOTONIC, &st.start)) err(1, "clock_gettime"); } static void getfdtype(IO *io) { struct stat sb; int type; if (fstat(io->fd, &sb) == -1) err(1, "%s", io->name); if (S_ISREG(sb.st_mode)) io->flags |= ISTRUNC; if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) { if (ioctl(io->fd, FIODTYPE, &type) == -1) { err(1, "%s", io->name); } else { if (type & D_TAPE) io->flags |= ISTAPE; else if (type & (D_DISK | D_MEM)) io->flags |= ISSEEK; if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0) io->flags |= ISCHR; } return; } errno = 0; if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) io->flags |= ISPIPE; else io->flags |= ISSEEK; } /* * Limit the speed by adding a delay before every block read. * The delay (t_usleep) is equal to the time computed from block * size and the specified speed limit (t_target) minus the time * spent on actual read and write operations (t_io). */ static void speed_limit(void) { static double t_prev, t_usleep; double t_now, t_io, t_target; t_now = secs_elapsed(); t_io = t_now - t_prev - t_usleep; t_target = (double)in.dbsz / (double)speed; t_usleep = t_target - t_io; if (t_usleep > 0) usleep(t_usleep * 1000000); else t_usleep = 0; t_prev = t_now; } static void swapbytes(void *v, size_t len) { unsigned char *p = v; unsigned char t; while (len > 1) { t = p[0]; p[0] = p[1]; p[1] = t; p += 2; len -= 2; } } static void dd_in(void) { ssize_t n; for (;;) { switch (cpy_cnt) { case -1: /* count=0 was specified */ return; case 0: break; default: if (st.in_full + st.in_part >= (uintmax_t)cpy_cnt) return; break; } if (speed > 0) speed_limit(); /* * Zero the buffer first if sync; if doing block operations, * use spaces. */ if (ddflags & C_SYNC) { if (ddflags & C_FILL) memset(in.dbp, fill_char, in.dbsz); else if (ddflags & (C_BLOCK | C_UNBLOCK)) memset(in.dbp, ' ', in.dbsz); else memset(in.dbp, 0, in.dbsz); } n = read(in.fd, in.dbp, in.dbsz); if (n == 0) { in.dbrcnt = 0; return; } /* Read error. */ if (n == -1) { /* * If noerror not specified, die. POSIX requires that * the warning message be followed by an I/O display. */ if (!(ddflags & C_NOERROR)) err(1, "%s", in.name); warn("%s", in.name); summary(); /* * If it's a seekable file descriptor, seek past the * error. If your OS doesn't do the right thing for * raw disks this section should be modified to re-read * in sector size chunks. */ if (in.flags & ISSEEK && lseek(in.fd, (off_t)in.dbsz, SEEK_CUR)) warn("%s", in.name); /* If sync not specified, omit block and continue. */ if (!(ddflags & C_SYNC)) continue; /* Read errors count as full blocks. */ in.dbcnt += in.dbrcnt = in.dbsz; ++st.in_full; /* Handle full input blocks. */ } else if ((size_t)n == (size_t)in.dbsz) { in.dbcnt += in.dbrcnt = n; ++st.in_full; /* Handle partial input blocks. */ } else { /* If sync, use the entire block. */ if (ddflags & C_SYNC) in.dbcnt += in.dbrcnt = in.dbsz; else in.dbcnt += in.dbrcnt = n; ++st.in_part; } /* * POSIX states that if bs is set and no other conversions * than noerror, notrunc or sync are specified, the block * is output without buffering as it is read. */ if ((ddflags & ~(C_NOERROR | C_NOTRUNC | C_SYNC)) == C_BS) { out.dbcnt = in.dbcnt; dd_out(1); in.dbcnt = 0; continue; } if (ddflags & C_SWAB) { if ((n = in.dbrcnt) & 1) { ++st.swab; --n; } swapbytes(in.dbp, (size_t)n); } in.dbp += in.dbrcnt; (*cfunc)(); if (need_summary) summary(); if (need_progress) progress(); } } /* * Clean up any remaining I/O and flush output. If necessary, the output file * is truncated. */ static void dd_close(void) { if (cfunc == def) def_close(); else if (cfunc == block) block_close(); else if (cfunc == unblock) unblock_close(); if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) { if (ddflags & C_FILL) memset(out.dbp, fill_char, out.dbsz - out.dbcnt); else if (ddflags & (C_BLOCK | C_UNBLOCK)) memset(out.dbp, ' ', out.dbsz - out.dbcnt); else memset(out.dbp, 0, out.dbsz - out.dbcnt); out.dbcnt = out.dbsz; } if (out.dbcnt || pending) dd_out(1); /* * If the file ends with a hole, ftruncate it to extend its size * up to the end of the hole (without having to write any data). */ if (out.seek_offset > 0 && (out.flags & ISTRUNC)) { if (ftruncate(out.fd, out.seek_offset) == -1) err(1, "truncating %s", out.name); + } + + if (ddflags & C_FSYNC) { + if (fsync(out.fd) == -1) + err(1, "fsyncing %s", out.name); } } void dd_out(int force) { u_char *outp; size_t cnt, n; ssize_t nw; static int warned; int sparse; /* * Write one or more blocks out. The common case is writing a full * output block in a single write; increment the full block stats. * Otherwise, we're into partial block writes. If a partial write, * and it's a character device, just warn. If a tape device, quit. * * The partial writes represent two cases. 1: Where the input block * was less than expected so the output block was less than expected. * 2: Where the input block was the right size but we were forced to * write the block in multiple chunks. The original versions of dd(1) * never wrote a block in more than a single write, so the latter case * never happened. * * One special case is if we're forced to do the write -- in that case * we play games with the buffer size, and it's usually a partial write. */ outp = out.db; /* * If force, first try to write all pending data, else try to write * just one block. Subsequently always write data one full block at * a time at most. */ for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) { cnt = n; do { sparse = 0; if (ddflags & C_SPARSE) { /* Is buffer sparse? */ sparse = BISZERO(outp, cnt); } if (sparse && !force) { pending += cnt; nw = cnt; } else { if (pending != 0) { /* * Seek past hole. Note that we need to record the * reached offset, because we might have no more data * to write, in which case we'll need to call * ftruncate to extend the file size. */ out.seek_offset = lseek(out.fd, pending, SEEK_CUR); if (out.seek_offset == -1) err(2, "%s: seek error creating sparse file", out.name); pending = 0; } if (cnt) { nw = write(out.fd, outp, cnt); out.seek_offset = 0; } else { return; } } if (nw <= 0) { if (nw == 0) errx(1, "%s: end of device", out.name); if (errno != EINTR) err(1, "%s", out.name); nw = 0; } outp += nw; st.bytes += nw; if ((size_t)nw == n && n == (size_t)out.dbsz) ++st.out_full; else ++st.out_part; if ((size_t) nw != cnt) { if (out.flags & ISTAPE) errx(1, "%s: short write on tape device", out.name); if (out.flags & ISCHR && !warned) { warned = 1; warnx("%s: short write on character device", out.name); } } cnt -= nw; } while (cnt != 0); if ((out.dbcnt -= n) < out.dbsz) break; } /* Reassemble the output block. */ if (out.dbcnt) (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt); out.dbp = out.db + out.dbcnt; } Index: head/bin/dd/dd.h =================================================================== --- head/bin/dd/dd.h (revision 351769) +++ head/bin/dd/dd.h (revision 351770) @@ -1,109 +1,110 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1991, 1993, 1994 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Keith Muller of the University of California, San Diego and Lance * Visser of Convex Computer Corporation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)dd.h 8.3 (Berkeley) 4/2/94 * $FreeBSD$ */ /* Input/output stream state. */ typedef struct { u_char *db; /* buffer address */ u_char *dbp; /* current buffer I/O address */ ssize_t dbcnt; /* current buffer byte count */ ssize_t dbrcnt; /* last read byte count */ ssize_t dbsz; /* block size */ #define ISCHR 0x01 /* character device (warn on short) */ #define ISPIPE 0x02 /* pipe-like (see position.c) */ #define ISTAPE 0x04 /* tape */ #define ISSEEK 0x08 /* valid to seek on */ #define NOREAD 0x10 /* not readable */ #define ISTRUNC 0x20 /* valid to ftruncate() */ u_int flags; const char *name; /* name */ int fd; /* file descriptor */ off_t offset; /* # of blocks to skip */ off_t seek_offset; /* offset of last seek past output hole */ } IO; typedef struct { uintmax_t in_full; /* # of full input blocks */ uintmax_t in_part; /* # of partial input blocks */ uintmax_t out_full; /* # of full output blocks */ uintmax_t out_part; /* # of partial output blocks */ uintmax_t trunc; /* # of truncated records */ uintmax_t swab; /* # of odd-length swab blocks */ uintmax_t bytes; /* # of bytes written */ struct timespec start; /* start time of dd */ } STAT; /* Flags (in ddflags). */ #define C_ASCII 0x00000001 #define C_BLOCK 0x00000002 #define C_BS 0x00000004 #define C_CBS 0x00000008 #define C_COUNT 0x00000010 #define C_EBCDIC 0x00000020 #define C_FILES 0x00000040 #define C_IBS 0x00000080 #define C_IF 0x00000100 #define C_LCASE 0x00000200 #define C_NOERROR 0x00000400 #define C_NOTRUNC 0x00000800 #define C_OBS 0x00001000 #define C_OF 0x00002000 #define C_OSYNC 0x00004000 #define C_PAREVEN 0x00008000 #define C_PARNONE 0x00010000 #define C_PARODD 0x00020000 #define C_PARSET 0x00040000 #define C_SEEK 0x00080000 #define C_SKIP 0x00100000 #define C_SPARSE 0x00200000 #define C_SWAB 0x00400000 #define C_SYNC 0x00800000 #define C_UCASE 0x01000000 #define C_UNBLOCK 0x02000000 #define C_FILL 0x04000000 #define C_STATUS 0x08000000 #define C_NOXFER 0x10000000 #define C_NOINFO 0x20000000 #define C_PROGRESS 0x40000000 +#define C_FSYNC 0x80000000 #define C_PARITY (C_PAREVEN | C_PARODD | C_PARNONE | C_PARSET) #define BISZERO(p, s) ((s) > 0 && *((const char *)p) == 0 && !memcmp( \ (const void *)(p), (const void *) \ ((const char *)p + 1), (s) - 1))