LCOV - code coverage report
Current view: top level - extmod - vfs_blockdev.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.19.1-724-gfb7d21153.info Lines: 53 57 93.0 %
Date: 2022-12-01 09:37:31 Functions: 6 6 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                 :        172 : void mp_vfs_blockdev_init(mp_vfs_blockdev_t *self, mp_obj_t bdev) {
      36                 :        172 :     mp_load_method(bdev, MP_QSTR_readblocks, self->readblocks);
      37                 :        172 :     mp_load_method_maybe(bdev, MP_QSTR_writeblocks, self->writeblocks);
      38                 :        172 :     mp_load_method_maybe(bdev, MP_QSTR_ioctl, self->u.ioctl);
      39         [ +  + ]:        172 :     if (self->u.ioctl[0] != MP_OBJ_NULL) {
      40                 :            :         // Device supports new block protocol, so indicate it
      41                 :        168 :         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                 :        172 : }
      48                 :            : 
      49                 :        446 : int mp_vfs_blockdev_read(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, uint8_t *buf) {
      50         [ -  + ]:        446 :     if (self->flags & MP_BLOCKDEV_FLAG_NATIVE) {
      51                 :          0 :         mp_uint_t (*f)(uint8_t *, uint32_t, uint32_t) = (void *)(uintptr_t)self->readblocks[2];
      52                 :          0 :         return f(buf, block_num, num_blocks);
      53                 :            :     } else {
      54                 :        446 :         mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, num_blocks *self->block_size, buf};
      55                 :        446 :         self->readblocks[2] = MP_OBJ_NEW_SMALL_INT(block_num);
      56                 :        446 :         self->readblocks[3] = MP_OBJ_FROM_PTR(&ar);
      57                 :        446 :         mp_call_method_n_kw(2, 0, self->readblocks);
      58                 :            :         // TODO handle error return
      59                 :        446 :         return 0;
      60                 :            :     }
      61                 :            : }
      62                 :            : 
      63                 :      15564 : 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) {
      64                 :      15564 :     mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, len, buf};
      65                 :      15564 :     self->readblocks[2] = MP_OBJ_NEW_SMALL_INT(block_num);
      66                 :      15564 :     self->readblocks[3] = MP_OBJ_FROM_PTR(&ar);
      67                 :      15564 :     self->readblocks[4] = MP_OBJ_NEW_SMALL_INT(block_off);
      68                 :      15564 :     mp_obj_t ret = mp_call_method_n_kw(3, 0, self->readblocks);
      69         [ +  + ]:      15560 :     if (ret == mp_const_none) {
      70                 :            :         return 0;
      71                 :            :     } else {
      72                 :        912 :         return MP_OBJ_SMALL_INT_VALUE(ret);
      73                 :            :     }
      74                 :            : }
      75                 :            : 
      76                 :        826 : int mp_vfs_blockdev_write(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, const uint8_t *buf) {
      77         [ +  - ]:        826 :     if (self->writeblocks[0] == MP_OBJ_NULL) {
      78                 :            :         // read-only block device
      79                 :            :         return -MP_EROFS;
      80                 :            :     }
      81                 :            : 
      82         [ -  + ]:        826 :     if (self->flags & MP_BLOCKDEV_FLAG_NATIVE) {
      83                 :          0 :         mp_uint_t (*f)(const uint8_t *, uint32_t, uint32_t) = (void *)(uintptr_t)self->writeblocks[2];
      84                 :          0 :         return f(buf, block_num, num_blocks);
      85                 :            :     } else {
      86                 :        826 :         mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, num_blocks *self->block_size, (void *)buf};
      87                 :        826 :         self->writeblocks[2] = MP_OBJ_NEW_SMALL_INT(block_num);
      88                 :        826 :         self->writeblocks[3] = MP_OBJ_FROM_PTR(&ar);
      89                 :        826 :         mp_call_method_n_kw(2, 0, self->writeblocks);
      90                 :            :         // TODO handle error return
      91                 :        826 :         return 0;
      92                 :            :     }
      93                 :            : }
      94                 :            : 
      95                 :       1190 : 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) {
      96         [ +  + ]:       1190 :     if (self->writeblocks[0] == MP_OBJ_NULL) {
      97                 :            :         // read-only block device
      98                 :            :         return -MP_EROFS;
      99                 :            :     }
     100                 :            : 
     101                 :       1186 :     mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, len, (void *)buf};
     102                 :       1186 :     self->writeblocks[2] = MP_OBJ_NEW_SMALL_INT(block_num);
     103                 :       1186 :     self->writeblocks[3] = MP_OBJ_FROM_PTR(&ar);
     104                 :       1186 :     self->writeblocks[4] = MP_OBJ_NEW_SMALL_INT(block_off);
     105                 :       1186 :     mp_obj_t ret = mp_call_method_n_kw(3, 0, self->writeblocks);
     106         [ +  + ]:       1186 :     if (ret == mp_const_none) {
     107                 :            :         return 0;
     108                 :            :     } else {
     109                 :        314 :         return MP_OBJ_SMALL_INT_VALUE(ret);
     110                 :            :     }
     111                 :            : }
     112                 :            : 
     113                 :       1538 : mp_obj_t mp_vfs_blockdev_ioctl(mp_vfs_blockdev_t *self, uintptr_t cmd, uintptr_t arg) {
     114         [ +  + ]:       1538 :     if (self->flags & MP_BLOCKDEV_FLAG_HAVE_IOCTL) {
     115                 :            :         // New protocol with ioctl
     116                 :       1518 :         self->u.ioctl[2] = MP_OBJ_NEW_SMALL_INT(cmd);
     117                 :       1518 :         self->u.ioctl[3] = MP_OBJ_NEW_SMALL_INT(arg);
     118                 :       1518 :         return mp_call_method_n_kw(2, 0, self->u.ioctl);
     119                 :            :     } else {
     120                 :            :         // Old protocol with sync and count
     121      [ +  +  + ]:         20 :         switch (cmd) {
     122                 :          6 :             case MP_BLOCKDEV_IOCTL_SYNC:
     123         [ +  - ]:          6 :                 if (self->u.old.sync[0] != MP_OBJ_NULL) {
     124                 :          6 :                     mp_call_method_n_kw(0, 0, self->u.old.sync);
     125                 :            :                 }
     126                 :            :                 break;
     127                 :            : 
     128                 :          2 :             case MP_BLOCKDEV_IOCTL_BLOCK_COUNT:
     129                 :          2 :                 return mp_call_method_n_kw(0, 0, self->u.old.count);
     130                 :            : 
     131                 :            :             case MP_BLOCKDEV_IOCTL_BLOCK_SIZE:
     132                 :            :                 // Old protocol has fixed sector size of 512 bytes
     133                 :            :                 break;
     134                 :            : 
     135                 :            :             case MP_BLOCKDEV_IOCTL_INIT:
     136                 :            :                 // Old protocol doesn't have init
     137                 :            :                 break;
     138                 :            :         }
     139                 :         18 :         return mp_const_none;
     140                 :            :     }
     141                 :            : }
     142                 :            : 
     143                 :            : #endif // MICROPY_VFS

Generated by: LCOV version 1.15-5-g462f71d