Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:14

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup rtems_disk Block Device Disk Management
0007  *
0008  * @brief Block Device Disk Management Initialize
0009  */
0010 
0011 /*
0012  * Copyright (c) 2012 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifdef HAVE_CONFIG_H
0037 #include "config.h"
0038 #endif
0039 
0040 #include <rtems/blkdev.h>
0041 #include <rtems/bdbuf.h>
0042 
0043 #include <string.h>
0044 
0045 rtems_status_code rtems_disk_init_phys(
0046   rtems_disk_device *dd,
0047   uint32_t block_size,
0048   rtems_blkdev_bnum block_count,
0049   rtems_block_device_ioctl handler,
0050   void *driver_data
0051 )
0052 {
0053   rtems_status_code sc;
0054 
0055   dd = memset(dd, 0, sizeof(*dd));
0056 
0057   dd->phys_dev = dd;
0058   dd->size = block_count;
0059   dd->media_block_size = block_size;
0060   dd->ioctl = handler;
0061   dd->driver_data = driver_data;
0062   dd->read_ahead.trigger = RTEMS_DISK_READ_AHEAD_NO_TRIGGER;
0063 
0064   if (block_count > 0) {
0065     if ((*handler)(dd, RTEMS_BLKIO_CAPABILITIES, &dd->capabilities) != 0) {
0066       dd->capabilities = 0;
0067     }
0068 
0069     sc = rtems_bdbuf_set_block_size(dd, block_size, false);
0070   } else {
0071     sc = RTEMS_INVALID_NUMBER;
0072   }
0073 
0074   return sc;
0075 }
0076 
0077 rtems_status_code rtems_disk_init_log(
0078   rtems_disk_device *dd,
0079   rtems_disk_device *phys_dd,
0080   rtems_blkdev_bnum block_begin,
0081   rtems_blkdev_bnum block_count
0082 )
0083 {
0084   rtems_status_code sc;
0085 
0086   dd = memset(dd, 0, sizeof(*dd));
0087 
0088   dd->phys_dev = phys_dd;
0089   dd->start = block_begin;
0090   dd->size = block_count;
0091   dd->media_block_size = phys_dd->media_block_size;
0092   dd->ioctl = phys_dd->ioctl;
0093   dd->driver_data = phys_dd->driver_data;
0094   dd->read_ahead.trigger = RTEMS_DISK_READ_AHEAD_NO_TRIGGER;
0095 
0096   if (phys_dd->phys_dev == phys_dd) {
0097     rtems_blkdev_bnum phys_block_count = phys_dd->size;
0098 
0099     if (
0100       block_begin < phys_block_count
0101         && block_count > 0
0102         && block_count <= phys_block_count - block_begin
0103     ) {
0104       sc = rtems_bdbuf_set_block_size(dd, phys_dd->media_block_size, false);
0105     } else {
0106       sc = RTEMS_INVALID_NUMBER;
0107     }
0108   } else {
0109     sc = RTEMS_INVALID_ID;
0110   }
0111 
0112   return sc;
0113 }