KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
sd.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dc/sd.h
4 Copyright (C) 2012 Lawrence Sebald
5*/
6
7/** \file dc/sd.h
8 \brief Block-level access to an SD card attached to the SCIF port.
9 \ingroup vfs_sd
10
11 This file contains the interface to working with the SD card reader that was
12 designed by jj1odm. The SD card reader itself connects to the SCIF port and
13 uses it basically as a simple SPI bus.
14
15 For reference, all I/O through this code should be done in the order of SD
16 card blocks (which are 512 bytes a piece). Also, this should adequately
17 support SD and SDHC cards (and possibly SDXC, but I don't have any of them
18 to try out).
19
20 This code doesn't directly implement any filesystems on top of the SD card,
21 but rather provides you with direct block-level access. This probably will
22 not be useful to most people in its current form (without a filesystem), but
23 this will provide you with all of the building blocks you should need to
24 actually make it work for you.
25
26 Due to the patent-encumbered nature of certain parts of the FAT32
27 filesystem, that filesystem will likely never be supported in KOS proper
28 (unless, of course, people are still using KOS after those patents expire).
29 I'm not going to encourage anyone to violate Microsoft's patents on FAT32
30 and I'm not going to be the enabler for anyone to do so either. So, if you
31 want FAT32, you're on your own.
32
33 \author Lawrence Sebald
34 \see dc/scif.h
35*/
36
37#ifndef __DC_SD_H
38#define __DC_SD_H
39
40#include <sys/cdefs.h>
41__BEGIN_DECLS
42
43#include <arch/types.h>
44#include <kos/blockdev.h>
45
46/** \defgroup vfs_sd SD Card
47 \brief VFS driver for accessing SD cards over the SCIF port
48 \ingroup vfs
49
50 @{
51*/
52
53/** \brief Calculate a SD/MMC-style CRC over a block of data.
54
55 This function calculates a 7-bit CRC over a given block of data. The
56 polynomial of this CRC is x^7 + x^3 + 1. The CRC is shifted into the upper
57 7 bits of the return value, so bit 0 should always be 0.
58
59 \param data The block of data to calculate the CRC over.
60 \param size The number of bytes in the block of data.
61 \param crc The starting value of the calculation. If you're
62 passing in a full block, this will probably be 0.
63 \return The calculated CRC.
64*/
65uint8 sd_crc7(const uint8 *data, int size, uint8 crc);
66
67/** \brief Initialize the SD card for use.
68
69 This function initializes the SD card for first use. This includes all steps
70 of the basic initialization sequence for SPI mode, as documented in the SD
71 card spec and at http://elm-chan.org/docs/mmc/mmc_e.html . This also will
72 call scif_sd_init() for you, so you don't have to worry about that ahead of
73 time.
74
75 \retval 0 On success.
76 \retval -1 On failure. This could indicate any number of
77 problems, but probably means that no SD card was
78 detected.
79*/
80int sd_init(void);
81
82/** \brief Shut down SD card support.
83
84 This function shuts down SD card support, and cleans up anything that the
85 sd_init() function set up.
86
87 \retval 0 On success.
88 \retval -1 On failure. The only currently defined error is if
89 the card was never initialized to start with.
90*/
91int sd_shutdown(void);
92
93/** \brief Read one or more blocks from the SD card.
94
95 This function reads the specified number of blocks from the SD card from the
96 beginning block specified into the buffer passed in. It is your
97 responsibility to allocate the buffer properly for the number of bytes that
98 is to be read (512 * the number of blocks requested).
99
100 \param block The starting block number to read from.
101 \param count The number of 512 byte blocks of data to read.
102 \param buf The buffer to read into.
103 \retval 0 On success.
104 \retval -1 On error, errno will be set as appropriate.
105
106 \par Error Conditions:
107 \em EIO - an I/O error occurred in reading data \n
108 \em ENXIO - SD card support was not initialized
109*/
110int sd_read_blocks(uint32 block, size_t count, uint8 *buf);
111
112/** \brief Write one or more blocks to the SD card.
113
114 This function writes the specified number of blocks to the SD card at the
115 beginning block specified from the buffer passed in. Each block is 512 bytes
116 in length, and you must write at least one block at a time. You cannot write
117 partial blocks.
118
119 If this function returns an error, you have quite possibly corrupted
120 something on the card or have a damaged card in general (unless errno is
121 ENXIO).
122
123 \param block The starting block number to write to.
124 \param count The number of 512 byte blocks of data to write.
125 \param buf The buffer to write from.
126 \retval 0 On success.
127 \retval -1 On error, errno will be set as appropriate.
128
129 \par Error Conditions:
130 \em EIO - an I/O error occurred in reading data \n
131 \em ENXIO - SD card support was not initialized
132*/
133int sd_write_blocks(uint32 block, size_t count, const uint8 *buf);
134
135/** \brief Retrieve the size of the SD card.
136
137 This function reads the size of the SD card from the card's CSD register.
138 This is the raw size of the card, not its formatted capacity. To get the
139 number of blocks from this, divide by 512.
140
141 \return On success, the raw size of the SD card in bytes. On
142 error, (uint64)-1.
143
144 \par Error Conditions:
145 \em EIO - an I/O error occurred in reading data \n
146 \em ENXIO - SD card support was not initialized
147*/
149
150/** \brief Get a block device for a given partition on the SD card.
151
152 This function creates a block device descriptor for the given partition on
153 the attached SD card. This block device is used to interface with various
154 filesystems on the device.
155
156 \param partition The partition number (0-3) to use.
157 \param rv Used to return the block device. Must be non-NULL.
158 \param partition_type Used to return the partition type. Must be non-NULL.
159 \retval 0 On success.
160 \retval -1 On error, errno will be set as appropriate.
161
162 \par Error Conditions:
163 \em ENXIO - SD card support was not initialized \n
164 \em EIO - an I/O error occurred in reading data \n
165 \em EINVAL - invalid partition number was given \n
166 \em EFAULT - rv or partition_type was NULL \n
167 \em ENOENT - no MBR found \n
168 \em ENOENT - no partition at the specified position \n
169 \em ENOMEM - out of memory
170
171 \note This interface currently only supports MBR-formatted SD cards. There
172 is currently no support for GPT partition tables.
173*/
175 uint8 *partition_type);
176
177/** @} */
178
179__END_DECLS
180#endif /* !__DC_SD_H */
Definitions for a simple block device interface.
unsigned long long uint64
64-bit unsigned integer
Definition types.h:32
unsigned long uint32
32-bit unsigned integer
Definition types.h:33
unsigned char uint8
8-bit unsigned integer
Definition types.h:35
int sd_write_blocks(uint32 block, size_t count, const uint8 *buf)
Write one or more blocks to the SD card.
uint64 sd_get_size(void)
Retrieve the size of the SD card.
int sd_init(void)
Initialize the SD card for use.
uint8 sd_crc7(const uint8 *data, int size, uint8 crc)
Calculate a SD/MMC-style CRC over a block of data.
int sd_read_blocks(uint32 block, size_t count, uint8 *buf)
Read one or more blocks from the SD card.
int sd_shutdown(void)
Shut down SD card support.
int sd_blockdev_for_partition(int partition, kos_blockdev_t *rv, uint8 *partition_type)
Get a block device for a given partition on the SD card.
A simple block device.
Definition blockdev.h:54
Common integer types.