LCOV - code coverage report
Current view: top level - extmod - vfs_blockdev.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.24.0-7-g548babf8a.info Lines: 42 46 91.3 %
Date: 2024-10-30 09:06:48 Functions: 7 7 100.0 %
Branches: 17 21 81.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                 :            :         // Block device functions are expected to return 0 on success
      62                 :            :         // and negative integer on errors. Check for positive integer
      63                 :            :         // results as some callers (i.e. littlefs) will produce corrupt
      64                 :            :         // results from these.
      65                 :       1456 :         int i = MP_OBJ_SMALL_INT_VALUE(ret);
      66         [ +  + ]:       1456 :         return i > 0 ? (-MP_EINVAL) : i;
      67                 :            :     }
      68                 :            : }
      69                 :            : 
      70                 :        488 : int mp_vfs_blockdev_read(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, uint8_t *buf) {
      71         [ -  + ]:        488 :     if (self->flags & MP_BLOCKDEV_FLAG_NATIVE) {
      72                 :          0 :         mp_uint_t (*f)(uint8_t *, uint32_t, uint32_t) = (void *)(uintptr_t)self->readblocks[2];
      73                 :          0 :         return f(buf, block_num, num_blocks);
      74                 :            :     } else {
      75                 :        488 :         return mp_vfs_blockdev_call_rw(self->readblocks, block_num, 0, num_blocks * self->block_size, buf, 2);
      76                 :            :     }
      77                 :            : }
      78                 :            : 
      79                 :      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) {
      80                 :      17996 :     return mp_vfs_blockdev_call_rw(self->readblocks, block_num, block_off, len, buf, 3);
      81                 :            : }
      82                 :            : 
      83                 :        854 : int mp_vfs_blockdev_write(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, const uint8_t *buf) {
      84         [ +  - ]:        854 :     if (self->writeblocks[0] == MP_OBJ_NULL) {
      85                 :            :         // read-only block device
      86                 :            :         return -MP_EROFS;
      87                 :            :     }
      88                 :            : 
      89         [ -  + ]:        854 :     if (self->flags & MP_BLOCKDEV_FLAG_NATIVE) {
      90                 :          0 :         mp_uint_t (*f)(const uint8_t *, uint32_t, uint32_t) = (void *)(uintptr_t)self->writeblocks[2];
      91                 :          0 :         return f(buf, block_num, num_blocks);
      92                 :            :     } else {
      93                 :        854 :         return mp_vfs_blockdev_call_rw(self->writeblocks, block_num, 0, num_blocks * self->block_size, (void *)buf, 2);
      94                 :            :     }
      95                 :            : }
      96                 :            : 
      97                 :       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) {
      98         [ +  + ]:       1194 :     if (self->writeblocks[0] == MP_OBJ_NULL) {
      99                 :            :         // read-only block device
     100                 :            :         return -MP_EROFS;
     101                 :            :     }
     102                 :       1190 :     return mp_vfs_blockdev_call_rw(self->writeblocks, block_num, block_off, len, (void *)buf, 3);
     103                 :            : }
     104                 :            : 
     105                 :       1584 : mp_obj_t mp_vfs_blockdev_ioctl(mp_vfs_blockdev_t *self, uintptr_t cmd, uintptr_t arg) {
     106         [ +  + ]:       1584 :     if (self->flags & MP_BLOCKDEV_FLAG_HAVE_IOCTL) {
     107                 :            :         // New protocol with ioctl
     108                 :       1564 :         self->u.ioctl[2] = MP_OBJ_NEW_SMALL_INT(cmd);
     109                 :       1564 :         self->u.ioctl[3] = MP_OBJ_NEW_SMALL_INT(arg);
     110                 :       1564 :         return mp_call_method_n_kw(2, 0, self->u.ioctl);
     111                 :            :     } else {
     112                 :            :         // Old protocol with sync and count
     113      [ +  +  + ]:         20 :         switch (cmd) {
     114                 :          6 :             case MP_BLOCKDEV_IOCTL_SYNC:
     115         [ +  - ]:          6 :                 if (self->u.old.sync[0] != MP_OBJ_NULL) {
     116                 :          6 :                     mp_call_method_n_kw(0, 0, self->u.old.sync);
     117                 :            :                 }
     118                 :            :                 break;
     119                 :            : 
     120                 :          2 :             case MP_BLOCKDEV_IOCTL_BLOCK_COUNT:
     121                 :          2 :                 return mp_call_method_n_kw(0, 0, self->u.old.count);
     122                 :            : 
     123                 :            :             case MP_BLOCKDEV_IOCTL_BLOCK_SIZE:
     124                 :            :                 // Old protocol has fixed sector size of 512 bytes
     125                 :            :                 break;
     126                 :            : 
     127                 :            :             case MP_BLOCKDEV_IOCTL_INIT:
     128                 :            :                 // Old protocol doesn't have init
     129                 :            :                 break;
     130                 :            :         }
     131                 :         18 :         return mp_const_none;
     132                 :            :     }
     133                 :            : }
     134                 :            : 
     135                 :            : #endif // MICROPY_VFS

Generated by: LCOV version 1.15-5-g462f71d