2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28 #include <ipxe/fault.h>
37 * Inject fault with a specified probability
39 * @v rate Reciprocal of fault probability (must be non-zero)
40 * @ret rc Return status code
42 int inject_fault_nonzero ( unsigned int rate ) {
44 /* Do nothing unless we want to inject a fault now */
45 if ( ( random() % rate ) != 0 )
48 /* Generate error number here so that faults can be injected
49 * into files that don't themselves have error file
50 * identifiers (via errfile.h).
56 * Corrupt data with a specified probability
58 * @v rate Reciprocal of fault probability (must be non-zero)
60 * @v len Length of data
61 * @ret rc Return status code
63 void inject_corruption_nonzero ( unsigned int rate, const void *data,
68 /* Do nothing if we have no data to corrupt */
72 /* Do nothing unless we want to inject a fault now */
73 if ( ! inject_fault_nonzero ( rate ) )
76 /* Get a writable pointer to the nominally read-only data */
77 writable = ( ( uint8_t * ) data );
79 /* Pick a random victim byte and zap it */
80 offset = ( random() % len );
81 writable[offset] ^= random();