LCOV - code coverage report
Current view: top level - extmod - vfs_blockdev.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.24.0-161-gc73204128.info Lines: 44 48 91.7 %
Date: 2024-12-23 07:29:41 Functions: 7 7 100.0 %
Branches: 19 25 76.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython project, http://micropython.org/
       3                 :            :  *
       4                 :            :  * The MIT License (MIT)
       5                 :            :  *
       6                 :            :  * Copyright (c) 2013-2019 Damien P. George
       7                 :            :  *
       8                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       9                 :            :  * of this software and associated documentation files (the "Software"), to deal
      10                 :            :  * in the Software without restriction, including without limitation the rights
      11                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12                 :            :  * copies of the Software, and to permit persons to whom the Software is
      13                 :            :  * furnished to do so, subject to the following conditions:
      14                 :            :  *
      15                 :            :  * The above copyright notice and this permission notice shall be included in
      16                 :            :  * all copies or substantial portions of the Software.
      17                 :            :  *
      18                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      24                 :            :  * THE SOFTWARE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include "py/runtime.h"
      28                 :            : #include "py/binary.h"
      29                 :            : #include "py/objarray.h"
      30                 :            : #include "py/mperrno.h"
      31                 :            : #include "extmod/vfs.h"
      32                 :            : 
      33                 :            : #if MICROPY_VFS
      34                 :            : 
      35                 :        180 : void mp_vfs_blockdev_init(mp_vfs_blockdev_t *self, mp_obj_t bdev) {
      36                 :        180 :     mp_load_method(bdev, MP_QSTR_readblocks, self->readblocks);
      37                 :        180 :     mp_load_method_maybe(bdev, MP_QSTR_writeblocks, self->writeblocks);
      38                 :        180 :     mp_load_method_maybe(bdev, MP_QSTR_ioctl, self->u.ioctl);
      39         [ +  + ]:        180 :     if (self->u.ioctl[0] != MP_OBJ_NULL) {
      40                 :            :         // Device supports new block protocol, so indicate it
      41                 :        176 :         self->flags |= MP_BLOCKDEV_FLAG_HAVE_IOCTL;
      42                 :            :     } else {
      43                 :            :         // No ioctl method, so assume the device uses the old block protocol
      44                 :          4 :         mp_load_method_maybe(bdev, MP_QSTR_sync, self->u.old.sync);
      45                 :          4 :         mp_load_method(bdev, MP_QSTR_count, self->u.old.count);
      46                 :            :     }
      47                 :        180 : }
      48                 :            : 
      49                 :            : // Helper function to minimise code size of read/write functions
      50                 :            : // note the n_args argument is moved to the end for further code size reduction (args keep same position in caller and callee).
      51                 :      20528 : static int mp_vfs_blockdev_call_rw(mp_obj_t *args, size_t block_num, size_t block_off, size_t len, void *buf, size_t n_args) {
      52                 :      20528 :     mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, len, buf};
      53                 :      20528 :     args[2] = MP_OBJ_NEW_SMALL_INT(block_num);
      54                 :      20528 :     args[3] = MP_OBJ_FROM_PTR(&ar);
      55                 :      20528 :     args[4] = MP_OBJ_NEW_SMALL_INT(block_off); // ignored for n_args == 2
      56                 :      20528 :     mp_obj_t ret = mp_call_method_n_kw(n_args, 0, args);
      57                 :            : 
      58         [ +  + ]:      20524 :     if (ret == mp_const_none) {
      59                 :            :         return 0;
      60                 :            :     } else {
      61                 :            :         // Some block devices return a bool indicating success, so
      62                 :            :         // convert those to an errno integer code.
      63         [ +  - ]:       1456 :         if (ret == mp_const_true) {
      64                 :            :             return 0;
      65         [ +  - ]:       1456 :         } else if (ret == mp_const_false) {
      66                 :            :             return -MP_EIO;
      67                 :            :         }
      68                 :            :         // Block device functions are expected to return 0 on success
      69                 :            :         // and negative integer on errors. Check for positive integer
      70                 :            :         // results as some callers (i.e. littlefs) will produce corrupt
      71                 :            :         // results from these.
      72                 :       1456 :         int i = MP_OBJ_SMALL_INT_VALUE(ret);
      73         [ +  + ]:       1456 :         return i > 0 ? (-MP_EINVAL) : i;
      74                 :            :     }
      75                 :            : }
      76                 :            : 
      77                 :        488 : int mp_vfs_blockdev_read(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, uint8_t *buf) {
      78         [ -  + ]:        488 :     if (self->flags & MP_BLOCKDEV_FLAG_NATIVE) {
      79                 :          0 :         mp_uint_t (*f)(uint8_t *, uint32_t, uint32_t) = (void *)(uintptr_t)self->readblocks[2];
      80                 :          0 :         return f(buf, block_num, num_blocks);
      81                 :            :     } else {
      82                 :        488 :         return mp_vfs_blockdev_call_rw(self->readblocks, block_num, 0, num_blocks * self->block_size, buf, 2);
      83                 :            :     }
      84                 :            : }
      85                 :            : 
      86                 :      17996 : int mp_vfs_blockdev_read_ext(mp_vfs_blockdev_t *self, size_t block_num, size_t block_off, size_t len, uint8_t *buf) {
      87                 :      17996 :     return mp_vfs_blockdev_call_rw(self->readblocks, block_num, block_off, len, buf, 3);
      88                 :            : }
      89                 :            : 
      90                 :        854 : int mp_vfs_blockdev_write(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, const uint8_t *buf) {
      91         [ +  - ]:        854 :     if (self->writeblocks[0] == MP_OBJ_NULL) {
      92                 :            :         // read-only block device
      93                 :            :         return -MP_EROFS;
      94                 :            :     }
      95                 :            : 
      96         [ -  + ]:        854 :     if (self->flags & MP_BLOCKDEV_FLAG_NATIVE) {
      97                 :          0 :         mp_uint_t (*f)(const uint8_t *, uint32_t, uint32_t) = (void *)(uintptr_t)self->writeblocks[2];
      98                 :          0 :         return f(buf, block_num, num_blocks);
      99                 :            :     } else {
     100                 :        854 :         return mp_vfs_blockdev_call_rw(self->writeblocks, block_num, 0, num_blocks * self->block_size, (void *)buf, 2);
     101                 :            :     }
     102                 :            : }
     103                 :            : 
     104                 :       1194 : int mp_vfs_blockdev_write_ext(mp_vfs_blockdev_t *self, size_t block_num, size_t block_off, size_t len, const uint8_t *buf) {
     105         [ +  + ]:       1194 :     if (self->writeblocks[0] == MP_OBJ_NULL) {
     106                 :            :         // read-only block device
     107                 :            :         return -MP_EROFS;
     108                 :            :     }
     109                 :       1190 :     return mp_vfs_blockdev_call_rw(self->writeblocks, block_num, block_off, len, (void *)buf, 3);
     110                 :            : }
     111                 :            : 
     112                 :       1584 : mp_obj_t mp_vfs_blockdev_ioctl(mp_vfs_blockdev_t *self, uintptr_t cmd, uintptr_t arg) {
     113         [ +  + ]:       1584 :     if (self->flags & MP_BLOCKDEV_FLAG_HAVE_IOCTL) {
     114                 :            :         // New protocol with ioctl
     115                 :       1564 :         self->u.ioctl[2] = MP_OBJ_NEW_SMALL_INT(cmd);
     116                 :       1564 :         self->u.ioctl[3] = MP_OBJ_NEW_SMALL_INT(arg);
     117                 :       1564 :         return mp_call_method_n_kw(2, 0, self->u.ioctl);
     118                 :            :     } else {
     119                 :            :         // Old protocol with sync and count
     120      [ +  +  + ]:         20 :         switch (cmd) {
     121                 :          6 :             case MP_BLOCKDEV_IOCTL_SYNC:
     122         [ +  - ]:          6 :                 if (self->u.old.sync[0] != MP_OBJ_NULL) {
     123                 :          6 :                     mp_call_method_n_kw(0, 0, self->u.old.sync);
     124                 :            :                 }
     125                 :            :                 break;
     126                 :            : 
     127                 :          2 :             case MP_BLOCKDEV_IOCTL_BLOCK_COUNT:
     128                 :          2 :                 return mp_call_method_n_kw(0, 0, self->u.old.count);
     129                 :            : 
     130                 :            :             case MP_BLOCKDEV_IOCTL_BLOCK_SIZE:
     131                 :            :                 // Old protocol has fixed sector size of 512 bytes
     132                 :            :                 break;
     133                 :            : 
     134                 :            :             case MP_BLOCKDEV_IOCTL_INIT:
     135                 :            :                 // Old protocol doesn't have init
     136                 :            :                 break;
     137                 :            :         }
     138                 :         18 :         return mp_const_none;
     139                 :            :     }
     140                 :            : }
     141                 :            : 
     142                 :            : #endif // MICROPY_VFS

Generated by: LCOV version 1.15-5-g462f71d