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
35void mp_vfs_blockdev_init(mp_vfs_blockdev_t *self, mp_obj_t bdev) {
36 mp_load_method(bdev, MP_QSTR_readblocks, self->readblocks);
37 mp_load_method_maybe(bdev, MP_QSTR_writeblocks, self->writeblocks);
38 mp_load_method_maybe(bdev, MP_QSTR_ioctl, self->u.ioctl);
39 if (self->u.ioctl[0] != MP_OBJ_NULL) {
40 // Device supports new block protocol, so indicate it
41 self->flags |= MP_BLOCKDEV_FLAG_HAVE_IOCTL;
42 } else {
43 // No ioctl method, so assume the device uses the old block protocol
44 mp_load_method_maybe(bdev, MP_QSTR_sync, self->u.old.sync);
45 mp_load_method(bdev, MP_QSTR_count, self->u.old.count);
46 }
47}
48
49int mp_vfs_blockdev_read(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, uint8_t *buf) {
50 if (self->flags & MP_BLOCKDEV_FLAG_NATIVE) {
51 mp_uint_t (*f)(uint8_t *, uint32_t, uint32_t) = (void *)(uintptr_t)self->readblocks[2];
52 return f(buf, block_num, num_blocks);
53 } else {
54 mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, num_blocks *self->block_size, buf};
55 self->readblocks[2] = MP_OBJ_NEW_SMALL_INT(block_num);
56 self->readblocks[3] = MP_OBJ_FROM_PTR(&ar);
57 mp_call_method_n_kw(2, 0, self->readblocks);
58 // TODO handle error return
59 return 0;
60 }
61}
62
63int 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 mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, len, buf};
65 self->readblocks[2] = MP_OBJ_NEW_SMALL_INT(block_num);
66 self->readblocks[3] = MP_OBJ_FROM_PTR(&ar);
67 self->readblocks[4] = MP_OBJ_NEW_SMALL_INT(block_off);
68 mp_obj_t ret = mp_call_method_n_kw(3, 0, self->readblocks);
69 if (ret == mp_const_none) {
70 return 0;
71 } else {
72 return MP_OBJ_SMALL_INT_VALUE(ret);
73 }
74}
75
76int mp_vfs_blockdev_write(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, const uint8_t *buf) {
77 if (self->writeblocks[0] == MP_OBJ_NULL) {
78 // read-only block device
79 return -MP_EROFS;
80 }
81
82 if (self->flags & MP_BLOCKDEV_FLAG_NATIVE) {
83 mp_uint_t (*f)(const uint8_t *, uint32_t, uint32_t) = (void *)(uintptr_t)self->writeblocks[2];
84 return f(buf, block_num, num_blocks);
85 } else {
86 mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, num_blocks *self->block_size, (void *)buf};
87 self->writeblocks[2] = MP_OBJ_NEW_SMALL_INT(block_num);
88 self->writeblocks[3] = MP_OBJ_FROM_PTR(&ar);
89 mp_call_method_n_kw(2, 0, self->writeblocks);
90 // TODO handle error return
91 return 0;
92 }
93}
94
95int 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 if (self->writeblocks[0] == MP_OBJ_NULL) {
97 // read-only block device
98 return -MP_EROFS;
99 }
100
101 mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, len, (void *)buf};
102 self->writeblocks[2] = MP_OBJ_NEW_SMALL_INT(block_num);
103 self->writeblocks[3] = MP_OBJ_FROM_PTR(&ar);
104 self->writeblocks[4] = MP_OBJ_NEW_SMALL_INT(block_off);
105 mp_obj_t ret = mp_call_method_n_kw(3, 0, self->writeblocks);
106 if (ret == mp_const_none) {
107 return 0;
108 } else {
109 return MP_OBJ_SMALL_INT_VALUE(ret);
110 }
111}
112
113mp_obj_t mp_vfs_blockdev_ioctl(mp_vfs_blockdev_t *self, uintptr_t cmd, uintptr_t arg) {
114 if (self->flags & MP_BLOCKDEV_FLAG_HAVE_IOCTL) {
115 // New protocol with ioctl
116 self->u.ioctl[2] = MP_OBJ_NEW_SMALL_INT(cmd);
117 self->u.ioctl[3] = MP_OBJ_NEW_SMALL_INT(arg);
118 return mp_call_method_n_kw(2, 0, self->u.ioctl);
119 } else {
120 // Old protocol with sync and count
121 switch (cmd) {
122 case MP_BLOCKDEV_IOCTL_SYNC:
123 if (self->u.old.sync[0] != MP_OBJ_NULL) {
124 mp_call_method_n_kw(0, 0, self->u.old.sync);
125 }
126 break;
127
128 case MP_BLOCKDEV_IOCTL_BLOCK_COUNT:
129 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 return mp_const_none;
140 }
141}
142
143#endif // MICROPY_VFS
144