| 1 | /****************************************************************************** |
|---|
| 2 | * (c)2008-2010 Broadcom Corporation |
|---|
| 3 | * |
|---|
| 4 | * This program is the proprietary software of Broadcom Corporation and/or its licensors, |
|---|
| 5 | * and may only be used, duplicated, modified or distributed pursuant to the terms and |
|---|
| 6 | * conditions of a separate, written license agreement executed between you and Broadcom |
|---|
| 7 | * (an "Authorized License"). Except as set forth in an Authorized License, Broadcom grants |
|---|
| 8 | * no license (express or implied), right to use, or waiver of any kind with respect to the |
|---|
| 9 | * Software, and Broadcom expressly reserves all rights in and to the Software and all |
|---|
| 10 | * intellectual property rights therein. IF YOU HAVE NO AUTHORIZED LICENSE, THEN YOU |
|---|
| 11 | * HAVE NO RIGHT TO USE THIS SOFTWARE IN ANY WAY, AND SHOULD IMMEDIATELY |
|---|
| 12 | * NOTIFY BROADCOM AND DISCONTINUE ALL USE OF THE SOFTWARE. |
|---|
| 13 | * |
|---|
| 14 | * Except as expressly set forth in the Authorized License, |
|---|
| 15 | * |
|---|
| 16 | * 1. This program, including its structure, sequence and organization, constitutes the valuable trade |
|---|
| 17 | * secrets of Broadcom, and you shall use all reasonable efforts to protect the confidentiality thereof, |
|---|
| 18 | * and to use this information only in connection with your use of Broadcom integrated circuit products. |
|---|
| 19 | * |
|---|
| 20 | * 2. TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" |
|---|
| 21 | * AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES, REPRESENTATIONS OR |
|---|
| 22 | * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO |
|---|
| 23 | * THE SOFTWARE. BROADCOM SPECIFICALLY DISCLAIMS ANY AND ALL IMPLIED WARRANTIES |
|---|
| 24 | * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, |
|---|
| 25 | * LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION |
|---|
| 26 | * OR CORRESPONDENCE TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING OUT OF |
|---|
| 27 | * USE OR PERFORMANCE OF THE SOFTWARE. |
|---|
| 28 | * |
|---|
| 29 | * 3. TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL BROADCOM OR ITS |
|---|
| 30 | * LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL, INCIDENTAL, SPECIAL, INDIRECT, OR |
|---|
| 31 | * EXEMPLARY DAMAGES WHATSOEVER ARISING OUT OF OR IN ANY WAY RELATING TO YOUR |
|---|
| 32 | * USE OF OR INABILITY TO USE THE SOFTWARE EVEN IF BROADCOM HAS BEEN ADVISED OF |
|---|
| 33 | * THE POSSIBILITY OF SUCH DAMAGES; OR (ii) ANY AMOUNT IN EXCESS OF THE AMOUNT |
|---|
| 34 | * ACTUALLY PAID FOR THE SOFTWARE ITSELF OR U.S. $1, WHICHEVER IS GREATER. THESE |
|---|
| 35 | * LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY FAILURE OF ESSENTIAL PURPOSE OF |
|---|
| 36 | * ANY LIMITED REMEDY. |
|---|
| 37 | * |
|---|
| 38 | * $brcm_Workfile: fileio_custom.c $ |
|---|
| 39 | * $brcm_Revision: 4 $ |
|---|
| 40 | * $brcm_Date: 9/29/10 11:58a $ |
|---|
| 41 | * |
|---|
| 42 | * Module Description: |
|---|
| 43 | * |
|---|
| 44 | * Revision History: |
|---|
| 45 | * |
|---|
| 46 | * $brcm_Log: /nexus/utils/fileio_custom.c $ |
|---|
| 47 | * |
|---|
| 48 | * 4 9/29/10 11:58a erickson |
|---|
| 49 | * SW7420-1123: add missing #include |
|---|
| 50 | * |
|---|
| 51 | * 3 4/2/10 2:37p erickson |
|---|
| 52 | * SW7405-3833: nexus_file_pvr.h is now part of the public API |
|---|
| 53 | * |
|---|
| 54 | * 2 2/23/10 4:50p vsilyaev |
|---|
| 55 | * SW3556-913: Added code that monitors state of the playback file and |
|---|
| 56 | * restarts playback (from last good position) in case of error |
|---|
| 57 | * |
|---|
| 58 | * 1 2/22/10 5:30p vsilyaev |
|---|
| 59 | * SW3556-913: Custom File I/O routines to inject errors |
|---|
| 60 | * |
|---|
| 61 | * |
|---|
| 62 | *****************************************************************************/ |
|---|
| 63 | #include "nexus_platform.h" |
|---|
| 64 | #include "bfile_io.h" |
|---|
| 65 | #include "nexus_file.h" |
|---|
| 66 | #include "nexus_file_pvr.h" |
|---|
| 67 | #include "bstd.h" |
|---|
| 68 | #include "bkni.h" |
|---|
| 69 | #include "fileio_custom.h" |
|---|
| 70 | |
|---|
| 71 | BDBG_MODULE(fileio_custom); |
|---|
| 72 | BDBG_OBJECT_ID(FileIoCustom); |
|---|
| 73 | BDBG_OBJECT_ID(FileIoCustomIo); |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | typedef struct FileIoCustom FileIoCustom; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | struct FileIoCustomIo { |
|---|
| 80 | struct bfile_io_read interface; /* must be first */ |
|---|
| 81 | unsigned count; |
|---|
| 82 | FileIoCustomProbabilities probabilities; |
|---|
| 83 | bfile_io_read_t parent; |
|---|
| 84 | FileIoCustom *master; |
|---|
| 85 | BDBG_OBJECT(FileIoCustomIo) |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | struct FileIoCustom { |
|---|
| 89 | struct NEXUS_FilePlay interface; /* must be the first member */ |
|---|
| 90 | NEXUS_FilePlayHandle parent; /* parent */ |
|---|
| 91 | struct FileIoCustomIo data; |
|---|
| 92 | struct FileIoCustomIo index; |
|---|
| 93 | BDBG_OBJECT(FileIoCustom) |
|---|
| 94 | }; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | static bool |
|---|
| 98 | b_test_dice(unsigned dice, unsigned probability) |
|---|
| 99 | { |
|---|
| 100 | /* BDBG_ERR(("%u %u", dice, probability)); */ |
|---|
| 101 | return (dice%100) < probability; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | #define B_FILE_TYPE(io) ((io == &io->master->index)?"INDEX":"DATA") |
|---|
| 105 | static ssize_t |
|---|
| 106 | b_custom_read(bfile_io_read_t fd, void *buf, size_t length) |
|---|
| 107 | { |
|---|
| 108 | struct FileIoCustomIo *io; |
|---|
| 109 | unsigned dice; |
|---|
| 110 | |
|---|
| 111 | io = (void*)fd; |
|---|
| 112 | BDBG_OBJECT_ASSERT(io, FileIoCustomIo); |
|---|
| 113 | |
|---|
| 114 | io->count++; |
|---|
| 115 | dice = io->count; |
|---|
| 116 | |
|---|
| 117 | if(b_test_dice(dice, io->probabilities.error)) { |
|---|
| 118 | BDBG_WRN(("%s:returning error", B_FILE_TYPE(io))); |
|---|
| 119 | return -1; |
|---|
| 120 | } |
|---|
| 121 | if(b_test_dice(dice, io->probabilities.nodata)) { |
|---|
| 122 | BDBG_WRN(("%s:returning no-data", B_FILE_TYPE(io))); |
|---|
| 123 | return BFILE_ERROR_NO_DATA; |
|---|
| 124 | } |
|---|
| 125 | if(b_test_dice(dice, io->probabilities.partial_read) && length>=2) { |
|---|
| 126 | BDBG_WRN(("%s:partial read:%u:%u", B_FILE_TYPE(io), length, length/2)); |
|---|
| 127 | length /= 2; |
|---|
| 128 | } |
|---|
| 129 | return io->parent->read(io->parent, buf, length); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | static off_t |
|---|
| 133 | b_custom_seek(bfile_io_read_t fd, off_t offset, int whence) |
|---|
| 134 | { |
|---|
| 135 | struct FileIoCustomIo *io; |
|---|
| 136 | unsigned dice; |
|---|
| 137 | |
|---|
| 138 | io = (void*)fd; |
|---|
| 139 | BDBG_OBJECT_ASSERT(io, FileIoCustomIo); |
|---|
| 140 | |
|---|
| 141 | dice = io->count; |
|---|
| 142 | if(b_test_dice(dice, io->probabilities.error)) { |
|---|
| 143 | BDBG_WRN(("%s:seek returning error", B_FILE_TYPE(io))); |
|---|
| 144 | return -1; |
|---|
| 145 | } |
|---|
| 146 | return io->parent->seek(io->parent, offset, whence); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | static int |
|---|
| 150 | b_custom_bounds(bfile_io_read_t fd, off_t *first, off_t *last) |
|---|
| 151 | { |
|---|
| 152 | struct FileIoCustomIo *io; |
|---|
| 153 | unsigned dice; |
|---|
| 154 | |
|---|
| 155 | io = (void*)fd; |
|---|
| 156 | BDBG_OBJECT_ASSERT(io, FileIoCustomIo); |
|---|
| 157 | |
|---|
| 158 | dice = io->count; |
|---|
| 159 | if(b_test_dice(dice, io->probabilities.error)) { |
|---|
| 160 | BDBG_WRN(("%s:bounds returning error", B_FILE_TYPE(io))); |
|---|
| 161 | return -1; |
|---|
| 162 | } |
|---|
| 163 | return io->parent->bounds(io->parent, first, last); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | static void |
|---|
| 167 | FileIoCustomIo_Init(struct FileIoCustomIo *io, bfile_io_read_t parent, FileIoCustom *master) |
|---|
| 168 | { |
|---|
| 169 | BDBG_OBJECT_INIT(io, FileIoCustomIo); |
|---|
| 170 | io->parent = parent; |
|---|
| 171 | io->master = master; |
|---|
| 172 | io->count = 0; |
|---|
| 173 | io->interface.read = b_custom_read; |
|---|
| 174 | io->interface.seek = b_custom_seek; |
|---|
| 175 | io->interface.bounds = b_custom_bounds; |
|---|
| 176 | io->interface.priority = parent->priority; |
|---|
| 177 | BKNI_Memset(&io->probabilities, 0, sizeof(io->probabilities)); |
|---|
| 178 | return; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | static void |
|---|
| 182 | b_custom_close(struct NEXUS_FilePlay *f) |
|---|
| 183 | { |
|---|
| 184 | FileIoCustom *file = (void *)f; |
|---|
| 185 | BDBG_OBJECT_ASSERT(file, FileIoCustom); |
|---|
| 186 | file->parent->file.close(file->parent); |
|---|
| 187 | BDBG_OBJECT_DESTROY(file, FileIoCustom); |
|---|
| 188 | BKNI_Free(file); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | NEXUS_FilePlayHandle |
|---|
| 192 | FileIoCustom_Attach(NEXUS_FilePlayHandle parent) |
|---|
| 193 | { |
|---|
| 194 | FileIoCustom *file; |
|---|
| 195 | |
|---|
| 196 | file = BKNI_Malloc(sizeof(*file)); |
|---|
| 197 | if(!file) { (void)BERR_TRACE(BERR_OUT_OF_SYSTEM_MEMORY);return NULL;} |
|---|
| 198 | BDBG_OBJECT_INIT(file, FileIoCustom); |
|---|
| 199 | file->parent = parent; |
|---|
| 200 | FileIoCustomIo_Init(&file->data, parent->file.data, file); |
|---|
| 201 | file->interface.file.data = &file->data.interface; |
|---|
| 202 | file->interface.file.close = b_custom_close; |
|---|
| 203 | if(parent->file.index) { |
|---|
| 204 | FileIoCustomIo_Init(&file->index, parent->file.index, file); |
|---|
| 205 | file->interface.file.index = &file->index.interface; |
|---|
| 206 | } else { |
|---|
| 207 | file->interface.file.index = NULL; |
|---|
| 208 | } |
|---|
| 209 | return &file->interface; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | void |
|---|
| 213 | FileIoCustom_GetProbabilities(NEXUS_FilePlayHandle file_, FileIoCustomProbabilities *dataProbabilities, FileIoCustomProbabilities *indexProbabilities) |
|---|
| 214 | { |
|---|
| 215 | FileIoCustom *file; |
|---|
| 216 | |
|---|
| 217 | file = (void*)file_; |
|---|
| 218 | BDBG_OBJECT_ASSERT(file, FileIoCustom); |
|---|
| 219 | if(dataProbabilities) { |
|---|
| 220 | *dataProbabilities = file->data.probabilities; |
|---|
| 221 | } |
|---|
| 222 | if(indexProbabilities) { |
|---|
| 223 | *indexProbabilities = file->index.probabilities; |
|---|
| 224 | } |
|---|
| 225 | return; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | BERR_Code |
|---|
| 229 | FileIoCustom_SetProbabilities(NEXUS_FilePlayHandle file_, const FileIoCustomProbabilities *dataProbabilities, const FileIoCustomProbabilities *indexProbabilities) |
|---|
| 230 | { |
|---|
| 231 | FileIoCustom *file; |
|---|
| 232 | |
|---|
| 233 | file = (void*)file_; |
|---|
| 234 | BDBG_OBJECT_ASSERT(file, FileIoCustom); |
|---|
| 235 | if(dataProbabilities) { |
|---|
| 236 | file->data.probabilities = *dataProbabilities; |
|---|
| 237 | } |
|---|
| 238 | if(indexProbabilities) { |
|---|
| 239 | file->index.probabilities = *indexProbabilities; |
|---|
| 240 | } |
|---|
| 241 | return BERR_SUCCESS; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | BDBG_OBJECT_ID(FileIoSticky); |
|---|
| 245 | BDBG_OBJECT_ID(FileIoStickyIo); |
|---|
| 246 | |
|---|
| 247 | typedef struct FileIoSticky FileIoSticky; |
|---|
| 248 | |
|---|
| 249 | struct FileIoStickyIo { |
|---|
| 250 | struct bfile_io_read interface; /* must be first */ |
|---|
| 251 | bool failed; |
|---|
| 252 | bfile_io_read_t parent; |
|---|
| 253 | FileIoSticky *master; |
|---|
| 254 | BDBG_OBJECT(FileIoStickyIo) |
|---|
| 255 | }; |
|---|
| 256 | |
|---|
| 257 | struct FileIoSticky { |
|---|
| 258 | struct NEXUS_FilePlay interface; /* must be the first member */ |
|---|
| 259 | NEXUS_FilePlayHandle parent; /* parent */ |
|---|
| 260 | struct FileIoStickyIo data; |
|---|
| 261 | struct FileIoStickyIo index; |
|---|
| 262 | BDBG_OBJECT(FileIoSticky) |
|---|
| 263 | }; |
|---|
| 264 | |
|---|
| 265 | static ssize_t |
|---|
| 266 | b_sticky_read(bfile_io_read_t fd, void *buf, size_t length) |
|---|
| 267 | { |
|---|
| 268 | struct FileIoStickyIo *io; |
|---|
| 269 | ssize_t rc; |
|---|
| 270 | |
|---|
| 271 | io = (void*)fd; |
|---|
| 272 | BDBG_OBJECT_ASSERT(io, FileIoStickyIo); |
|---|
| 273 | |
|---|
| 274 | if(io->failed) { |
|---|
| 275 | return -1; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | rc = io->parent->read(io->parent, buf, length); |
|---|
| 279 | if(rc>=0) { |
|---|
| 280 | return rc; |
|---|
| 281 | } |
|---|
| 282 | if((io == &io->master->data) && rc==BFILE_ERROR_NO_DATA) { |
|---|
| 283 | return rc; |
|---|
| 284 | } |
|---|
| 285 | BDBG_WRN(("b_sticky_read: %s failed %d", B_FILE_TYPE(io), rc)); |
|---|
| 286 | io->failed = true; |
|---|
| 287 | return rc; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | static off_t |
|---|
| 291 | b_sticky_seek(bfile_io_read_t fd, off_t offset, int whence) |
|---|
| 292 | { |
|---|
| 293 | struct FileIoStickyIo *io; |
|---|
| 294 | off_t rc; |
|---|
| 295 | |
|---|
| 296 | io = (void*)fd; |
|---|
| 297 | BDBG_OBJECT_ASSERT(io, FileIoStickyIo); |
|---|
| 298 | |
|---|
| 299 | rc = io->parent->seek(io->parent, offset, whence); |
|---|
| 300 | if(rc<0) { |
|---|
| 301 | BDBG_WRN(("b_sticky_seek: %s failed %d", B_FILE_TYPE(io), rc)); |
|---|
| 302 | io->failed = true; |
|---|
| 303 | } |
|---|
| 304 | return rc; |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | static int |
|---|
| 308 | b_sticky_bounds(bfile_io_read_t fd, off_t *first, off_t *last) |
|---|
| 309 | { |
|---|
| 310 | struct FileIoStickyIo *io; |
|---|
| 311 | int rc; |
|---|
| 312 | |
|---|
| 313 | io = (void*)fd; |
|---|
| 314 | BDBG_OBJECT_ASSERT(io, FileIoStickyIo); |
|---|
| 315 | |
|---|
| 316 | rc = io->parent->bounds(io->parent, first, last); |
|---|
| 317 | if(rc<0) { |
|---|
| 318 | BDBG_WRN(("b_sticky_bounds: %s failed %d", B_FILE_TYPE(io), rc)); |
|---|
| 319 | io->failed = true; |
|---|
| 320 | } |
|---|
| 321 | return rc; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | static void |
|---|
| 325 | FileIoStickyIo_Init(struct FileIoStickyIo *io, bfile_io_read_t parent, FileIoSticky *master) |
|---|
| 326 | { |
|---|
| 327 | BDBG_OBJECT_INIT(io, FileIoStickyIo); |
|---|
| 328 | io->failed = false; |
|---|
| 329 | io->parent = parent; |
|---|
| 330 | io->master = master; |
|---|
| 331 | io->interface.read = b_sticky_read; |
|---|
| 332 | io->interface.seek = b_sticky_seek; |
|---|
| 333 | io->interface.bounds = b_sticky_bounds; |
|---|
| 334 | io->interface.priority = parent->priority; |
|---|
| 335 | return; |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | static void |
|---|
| 339 | b_sticky_close(struct NEXUS_FilePlay *f) |
|---|
| 340 | { |
|---|
| 341 | FileIoSticky *file = (void *)f; |
|---|
| 342 | BDBG_OBJECT_ASSERT(file, FileIoSticky); |
|---|
| 343 | file->parent->file.close(file->parent); |
|---|
| 344 | BDBG_OBJECT_DESTROY(file, FileIoSticky); |
|---|
| 345 | BKNI_Free(file); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | NEXUS_FilePlayHandle |
|---|
| 349 | FileIoSticky_Attach(NEXUS_FilePlayHandle parent) |
|---|
| 350 | { |
|---|
| 351 | FileIoSticky *file; |
|---|
| 352 | |
|---|
| 353 | file = BKNI_Malloc(sizeof(*file)); |
|---|
| 354 | if(!file) { (void)BERR_TRACE(BERR_OUT_OF_SYSTEM_MEMORY);return NULL;} |
|---|
| 355 | BDBG_OBJECT_INIT(file, FileIoSticky); |
|---|
| 356 | file->parent = parent; |
|---|
| 357 | FileIoStickyIo_Init(&file->data, parent->file.data, file); |
|---|
| 358 | file->interface.file.data = &file->data.interface; |
|---|
| 359 | file->interface.file.close = b_sticky_close; |
|---|
| 360 | if(parent->file.index) { |
|---|
| 361 | FileIoStickyIo_Init(&file->index, parent->file.index, file); |
|---|
| 362 | file->interface.file.index = &file->index.interface; |
|---|
| 363 | } else { |
|---|
| 364 | file->interface.file.index = NULL; |
|---|
| 365 | } |
|---|
| 366 | return &file->interface; |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | void |
|---|
| 370 | FileIoSticky_GetFailBit(NEXUS_FilePlayHandle file_, bool *failed) |
|---|
| 371 | { |
|---|
| 372 | FileIoSticky *file; |
|---|
| 373 | |
|---|
| 374 | file = (void*)file_; |
|---|
| 375 | BDBG_OBJECT_ASSERT(file, FileIoSticky); |
|---|
| 376 | *failed = file->data.failed || file->index.failed; |
|---|
| 377 | return; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | void |
|---|
| 381 | FileIoSticky_ClearFailBit(NEXUS_FilePlayHandle file_) |
|---|
| 382 | { |
|---|
| 383 | FileIoSticky *file; |
|---|
| 384 | |
|---|
| 385 | file = (void*)file_; |
|---|
| 386 | BDBG_OBJECT_ASSERT(file, FileIoSticky); |
|---|
| 387 | file->data.failed = false; |
|---|
| 388 | file->index.failed = false; |
|---|
| 389 | return; |
|---|
| 390 | } |
|---|
| 391 | |
|---|