Skip to content

Commit 0c7c618

Browse files
committed
Added helper functions to mount/unmount mass storage devices
Fixed Abbaye des Morts game controls Added missing _start / _end tags from ld script
1 parent d7c6376 commit 0c7c618

File tree

6 files changed

+111
-41
lines changed

6 files changed

+111
-41
lines changed

abbaye/src/game.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,11 @@ void control (struct hero *jean,uint *keyp) {
411411
}
412412

413413
if (event.type == SDL_CONTROLLERBUTTONDOWN) {
414-
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP || event.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
414+
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP || event.cbutton.button == SDL_CONTROLLER_BUTTON_B) {
415415
if ((jean->push[0] == 0) && (jean->jump == 0) && (jean->ducking == 0))
416416
jean->jump = 1;
417417
}
418-
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN || event.cbutton.button == SDL_CONTROLLER_BUTTON_B) {
418+
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN || event.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
419419
if ((jean->push[1] == 0) && (jean->jump == 0)) {
420420
jean->push[1] = 1;
421421
jean->ducking = 1;
@@ -440,9 +440,9 @@ void control (struct hero *jean,uint *keyp) {
440440
}
441441

442442
if (event.type == SDL_CONTROLLERBUTTONUP) {
443-
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP || event.cbutton.button == SDL_CONTROLLER_BUTTON_A)
443+
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP || event.cbutton.button == SDL_CONTROLLER_BUTTON_B)
444444
jean->push[0] = 0;
445-
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN || event.cbutton.button == SDL_CONTROLLER_BUTTON_B) {
445+
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN || event.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
446446
jean->push[1] = 0;
447447
jean->ducking = 0;
448448
}

abbaye/src/main.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
#include "SDL_image.h"
1313
#include "SDL_mixer.h"
1414

15-
#include "emmc.h"
16-
#include "ff.h"
17-
18-
FATFS FatFs;
15+
#include "kernel.h"
1916

2017
extern void startscreen(SDL_Window *screen,uint *state,uint *grapset,uint *fullscreen);
2118
extern void history(SDL_Window *screen,uint *state,uint *grapset,uint *fullscreen);
@@ -41,9 +38,7 @@ void main () {
4138
uint grapset = 1; /* 0-8bits, 1-16bits */
4239
uint fullscreen = 0; /* 0-Windowed,1-Fullscreen */
4340

44-
if (sd_card_init(NULL) == 0) {
45-
f_mount(&FatFs, "0:", 1);
46-
}
41+
mount("sd:");
4742

4843
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
4944

kernel/kernel.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
#include "uspi.h"
2929
#endif
3030

31+
#define KERNEL_MAJ 0
32+
#define KERNEL_MIN 1
33+
#define KERNEL_REV 1
34+
35+
#define KERNEL_NAME "raspberrypi"
36+
#define KERNEL_VERSION "0.1.1"
37+
#define KERNEL_STRING "raspberrypi 0.1.1"
38+
3139
#if defined(__cplusplus)
3240
extern "C" {
3341
#endif
@@ -36,6 +44,9 @@ int usb_init();
3644

3745
int keyboard_init();
3846

47+
int mount(const char *source);
48+
int umount(const char *target);
49+
3950
#if defined(__cplusplus)
4051
}
4152
#endif

kernel/raspberry.ld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ SECTIONS
2828
}
2929

3030
.ARM.exidx : {
31+
__exidx_start = ABSOLUTE(.);
3132
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
33+
__exidx_end = ABSOLUTE(.);
3234
}
3335

3436
. = ALIGN(8);

kernel/syscalls.c

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdlib.h>
1919
#include <unistd.h>
2020
#include <stdint.h>
21+
#include <string.h>
2122
#include <errno.h>
2223
#include <fcntl.h>
2324
#include <sys/stat.h>
@@ -30,7 +31,7 @@
3031
#include "uspi.h"
3132
#endif
3233

33-
extern struct emmc_block_dev emmc_dev;
34+
static struct emmc_block_dev *emmc_dev;
3435

3536
/* Definitions of physical drive number for each drive */
3637
#define MMC 0 /* Map MMC/SD card to drive number 0 */
@@ -39,6 +40,7 @@ extern struct emmc_block_dev emmc_dev;
3940
#define MAX_DESCRIPTORS 256
4041
#define RESERVED_DESCRIPTORS 3
4142

43+
static FATFS fat_fs[_VOLUMES];
4244
static FIL *file_descriptors[MAX_DESCRIPTORS];
4345

4446
void * __dso_handle;
@@ -284,6 +286,47 @@ int chdir(const char * path) {
284286
return rc == FR_OK ? 0 : -1;
285287
}
286288

289+
int ftruncate(int file, off_t length) {
290+
if (file < RESERVED_DESCRIPTORS || file >= MAX_DESCRIPTORS || file_descriptors[file] == NULL) {
291+
errno = EBADF;
292+
return -1;
293+
}
294+
295+
FRESULT rc = f_truncate(file_descriptors[file]);
296+
errno = FRESULT_to_errno(rc);
297+
return rc == FR_OK ? 0 : -1;
298+
}
299+
300+
static const char * _VOLUME_NAME[_VOLUMES] = {
301+
_VOLUME_STRS
302+
};
303+
304+
int mount(const char *source) {
305+
for (int i = 0; i < _VOLUMES; i++) {
306+
if (!strncasecmp(source, _VOLUME_NAME[i], strlen(_VOLUME_NAME[i]))) {
307+
FRESULT rc = f_mount(&fat_fs[i], source, 1);
308+
errno = FRESULT_to_errno(rc);
309+
return rc == FR_OK ? 0 : -1;
310+
}
311+
}
312+
313+
errno = EINVAL;
314+
return -1;
315+
}
316+
317+
int umount(const char *target) {
318+
for (int i = 0; i < _VOLUMES; i++) {
319+
if (!strncasecmp(target, _VOLUME_NAME[i], strlen(_VOLUME_NAME[i]))) {
320+
FRESULT rc = f_mount(NULL, target, 1);
321+
errno = FRESULT_to_errno(rc);
322+
return rc == FR_OK ? 0 : -1;
323+
}
324+
}
325+
326+
errno = EINVAL;
327+
return -1;
328+
}
329+
287330
void _fini() {
288331
while(1)
289332
;
@@ -302,23 +345,30 @@ int _gettimeofday(struct timeval *tv, struct timezone *tz) {
302345
/*-----------------------------------------------------------------------*/
303346

304347
DSTATUS disk_status (
305-
BYTE pdrv /* Physical drive nmuber to identify the drive */
348+
BYTE pdrv /* Physical drive number to identify the drive */
306349
)
307350
{
308351
switch (pdrv) {
309352
case MMC :
353+
if (emmc_dev == NULL)
354+
{
355+
return STA_NOINIT;
356+
}
310357
return 0;
311358

312359
#ifdef HAVE_USPI
313360
default :
314-
if (!USPiMassStorageDeviceAvailable())
361+
{
362+
int nDeviceIndex = pdrv - USB;
363+
if (nDeviceIndex < 0 || nDeviceIndex >= USPiMassStorageDeviceAvailable())
315364
{
316-
return STA_NOINIT;
365+
return STA_NODISK;
317366
}
318367
return 0;
368+
}
319369
#endif
320370
}
321-
return STA_NOINIT;
371+
return STA_NODISK;
322372
}
323373

324374

@@ -328,23 +378,33 @@ DSTATUS disk_status (
328378
/*-----------------------------------------------------------------------*/
329379

330380
DSTATUS disk_initialize (
331-
BYTE pdrv /* Physical drive nmuber to identify the drive */
381+
BYTE pdrv /* Physical drive number to identify the drive */
332382
)
333383
{
334384
switch (pdrv) {
335385
case MMC :
386+
if (emmc_dev == NULL)
387+
{
388+
if (sd_card_init((struct block_device **)&emmc_dev) != 0)
389+
{
390+
return STA_NOINIT;
391+
}
392+
}
336393
return 0;
337394

338395
#ifdef HAVE_USPI
339396
default :
340-
if (!USPiMassStorageDeviceAvailable())
397+
{
398+
int nDeviceIndex = pdrv - USB;
399+
if (nDeviceIndex < 0 || nDeviceIndex >= USPiMassStorageDeviceAvailable())
341400
{
342-
return STA_NOINIT;
401+
return STA_NODISK;
343402
}
344403
return 0;
404+
}
345405
#endif
346406
}
347-
return STA_NOINIT;
407+
return STA_NODISK;
348408
}
349409

350410

@@ -354,7 +414,7 @@ DSTATUS disk_initialize (
354414
/*-----------------------------------------------------------------------*/
355415

356416
DRESULT disk_read (
357-
BYTE pdrv, /* Physical drive nmuber to identify the drive */
417+
BYTE pdrv, /* Physical drive number to identify the drive */
358418
BYTE *buff, /* Data buffer to store read data */
359419
DWORD sector, /* Sector address in LBA */
360420
UINT count /* Number of sectors to read */
@@ -363,7 +423,7 @@ DRESULT disk_read (
363423
switch (pdrv) {
364424
case MMC :
365425
{
366-
size_t buf_size = count * emmc_dev.bd.block_size;
426+
size_t buf_size = count * emmc_dev->bd.block_size;
367427
if (sd_read(buff, buf_size, sector) < buf_size)
368428
{
369429
return RES_ERROR;
@@ -375,13 +435,14 @@ DRESULT disk_read (
375435
#ifdef HAVE_USPI
376436
default :
377437
{
378-
if (!USPiMassStorageDeviceAvailable())
438+
int nDeviceIndex = pdrv - USB;
439+
if (nDeviceIndex < 0 || nDeviceIndex >= USPiMassStorageDeviceAvailable())
379440
{
380441
return RES_PARERR;
381442
}
382443

383444
unsigned buf_size = count * USPI_BLOCK_SIZE;
384-
if (USPiMassStorageDeviceRead(sector * USPI_BLOCK_SIZE, buff, buf_size, pdrv - USB) < buf_size)
445+
if (USPiMassStorageDeviceRead(sector * USPI_BLOCK_SIZE, buff, buf_size, nDeviceIndex) < buf_size)
385446
{
386447
return RES_ERROR;
387448
}
@@ -401,7 +462,7 @@ DRESULT disk_read (
401462
/*-----------------------------------------------------------------------*/
402463

403464
DRESULT disk_write (
404-
BYTE pdrv, /* Physical drive nmuber to identify the drive */
465+
BYTE pdrv, /* Physical drive number to identify the drive */
405466
const BYTE *buff, /* Data to be written */
406467
DWORD sector, /* Sector address in LBA */
407468
UINT count /* Number of sectors to write */
@@ -410,7 +471,7 @@ DRESULT disk_write (
410471
switch (pdrv) {
411472
case MMC :
412473
{
413-
size_t buf_size = count * emmc_dev.bd.block_size;
474+
size_t buf_size = count * emmc_dev->bd.block_size;
414475
if (sd_write((uint8_t *)buff, buf_size, sector) < buf_size)
415476
{
416477
return RES_ERROR;
@@ -422,13 +483,14 @@ DRESULT disk_write (
422483
#ifdef HAVE_USPI
423484
default :
424485
{
425-
if (!USPiMassStorageDeviceAvailable())
486+
int nDeviceIndex = pdrv - USB;
487+
if (nDeviceIndex < 0 || nDeviceIndex >= USPiMassStorageDeviceAvailable())
426488
{
427489
return RES_PARERR;
428490
}
429491

430492
unsigned buf_size = count * USPI_BLOCK_SIZE;
431-
if (USPiMassStorageDeviceWrite(sector * USPI_BLOCK_SIZE, buff, buf_size, pdrv - USB) < buf_size)
493+
if (USPiMassStorageDeviceWrite(sector * USPI_BLOCK_SIZE, buff, buf_size, nDeviceIndex) < buf_size)
432494
{
433495
return RES_ERROR;
434496
}
@@ -447,7 +509,7 @@ DRESULT disk_write (
447509
/*-----------------------------------------------------------------------*/
448510

449511
DRESULT disk_ioctl (
450-
BYTE pdrv, /* Physical drive nmuber (0..) */
512+
BYTE pdrv, /* Physical drive number (0..) */
451513
BYTE cmd, /* Control code */
452514
void *buff /* Buffer to send/receive control data */
453515
)
@@ -460,24 +522,26 @@ DRESULT disk_ioctl (
460522
}
461523
if (cmd == GET_SECTOR_COUNT)
462524
{
463-
*(DWORD *)buff = emmc_dev.bd.num_blocks;
525+
*(DWORD *)buff = emmc_dev->bd.num_blocks;
464526
return RES_OK;
465527
}
466528
if (cmd == GET_SECTOR_SIZE)
467529
{
468-
*(DWORD *)buff = emmc_dev.bd.block_size;
530+
*(DWORD *)buff = emmc_dev->bd.block_size;
469531
return RES_OK;
470532
}
471533
if (cmd == GET_BLOCK_SIZE)
472534
{
473-
*(DWORD *)buff = emmc_dev.bd.block_size;
535+
*(DWORD *)buff = emmc_dev->bd.block_size;
474536
return RES_OK;
475537
}
476538
return RES_PARERR;
477539

478540
#ifdef HAVE_USPI
479541
default :
480-
if (!USPiMassStorageDeviceAvailable())
542+
{
543+
int nDeviceIndex = pdrv - USB;
544+
if (nDeviceIndex < 0 || nDeviceIndex >= USPiMassStorageDeviceAvailable())
481545
{
482546
return RES_PARERR;
483547
}
@@ -487,7 +551,7 @@ DRESULT disk_ioctl (
487551
}
488552
if (cmd == GET_SECTOR_COUNT)
489553
{
490-
*(DWORD *)buff = USPiMassStorageDeviceGetCapacity(pdrv - USB);
554+
*(DWORD *)buff = USPiMassStorageDeviceGetCapacity(nDeviceIndex);
491555
return RES_OK;
492556
}
493557
if (cmd == GET_SECTOR_SIZE)
@@ -501,6 +565,7 @@ DRESULT disk_ioctl (
501565
return RES_OK;
502566
}
503567
return RES_PARERR;
568+
}
504569
#endif
505570
}
506571

template/main.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdio.h>
1919
#include <stdlib.h>
2020
#include <string.h>
21+
#include <errno.h>
2122

2223
#include "kernel.h"
2324
#include "wiring.h"
@@ -52,7 +53,7 @@ __attribute__ ((interrupt ("IRQ"))) void interrupt_irq() {
5253
#endif
5354

5455
void main() {
55-
int rc, x, y, c;
56+
int x, y, c;
5657
struct timer_wait tw;
5758
int led_status = LOW;
5859

@@ -72,13 +73,9 @@ void main() {
7273
mvaddstr(1, 9, "**** RASPBERRY-PI ****");
7374
mvaddstr(3, 7, "BARE-METAL SYSTEM TEMPLATE\r\n");
7475

75-
if (sd_card_init(NULL) == 0) {
76-
if ((rc = f_mount(&FatFs, "0:", 1)) != FR_OK) {
77-
addstrf("\r\nSD CARD NOT MOUNTED (%d)\r\n", rc);
78-
}
76+
if (mount("sd:") != 0) {
77+
addstrf("\r\nSD CARD NOT MOUNTED (%s)\r\n", strerror(errno));
7978
}
80-
else
81-
addstr("\r\nSD CARD ERROR\r\n");
8279

8380
usb_init();
8481
if (keyboard_init() != 0) {

0 commit comments

Comments
 (0)