Files
rtems/cpukit/libmisc/shell/fdisk.c
Frank Kühndel a479686c11 shell: Remove not functioning fdisk mount/unmount
The shell has an 'fdisk' command which has sub-commands 'mount' and 'unmount'.
These two sub-commands have a bug which causes them to be not able
to mount anything. This proposed patch removes the buggy file
cpukit/libblock/src/bdpart-mount.c and the mount/unmount commands
from 'fdisk' as bug fix. The 'fdisk' command itself is not removed.
The reasons for removing the sub-commands (instead of fixing the issue) are:

  1) The bug has been introduced on 2010-May-31 with commit
     29e92b090c. Since ten years no one
     can use this feature, nor has anybody complained about it.

  2) Besides of the 'fdisk' 'mount' sub-command, the shell has the
     usual 'mount' and 'unmount' commands which can serve as
     substitutes.

  3) There are additional minor issues (see further down) which needed to
     be addressed when the file will be kept.

What follows below is the precise bug description.

The bug is in function rtems_bdpart_mount() which is only be used
by the 'fdisk' shell command to mount all partitions of a disk with a
single command:

> fdisk DISK_NAME mount
>         mounts the file system of each partition of the disk
>
> fdisk DISK_NAME unmount
>         unmounts the file system of each partition of the disk

The whole command does not work because in file
cpukit/libblock/src/bdpart-mount.c line 103 specifies the file system type
of each partition to be "msdos". Yet, "msdos" does not exist. The name
must be "dosfs".

Beside of this fundamental problem, there are more issues with the code
in bdpart-mount.c:

  1) The function returns RTEMS_SUCCESSFUL despite the mount always fails.

  2) The reason for errors is not written to the terminal.

  3) The directory '/mnt' is created but not deleted later on (failure or not).

  3) There is no documentation about this special 'fdisk' feature in the
     RTEMS Shell Guide ('fdisk' is mentioned but its documentation is a
     bit short):
     https://docs.rtems.org/branches/master/shell/
     file_and_directory.html#fdisk-format-disk

  4) Only "msdos" formatted partitions can be mounted and all partitions
     are mounted read-only. This is hard coded and cannot be changed by
     options. Moreover, there is no information about this to the user of
     the shell (i.e. using 'fdisk' mount requires insider knowledge).

How to reproduce:

  1) For testing, I use the 'testsuites/samples/fileio.exe' sample with qemu:

> cd rtems
> env QEMU_AUDIO_DRV="none" qemu-system-arm -net none -nographic \
> -M realview-pbx-a9 -m 256M -kernel \
> build/arm/realview_pbx_a9_qemu/testsuites/samples/fileio.exe

  2) Type any key to stop the timer and enter the sample tool.
     Type 's' to enter the shell, login as 'root' with the password
     shown in the terminal.

  3) Type the following shell commands (they create a RAM disk,
     partition it, register it, format it and try to mount it):

> mkrd
> fdisk /dev/rda fat32 16 write mbr
> fdisk /dev/rda register
> mkdos /dev/rda1
> fdisk /dev/rda mount

  4) The last line above is the command which fails - without an error
  message. There exists a '/mnt' directory but no '/mnt/rda1' directory
  as it should be:

> ls -la /mnt

  5) If you change line 103 of 'cpukit/libblock/src/bdpart-mount.c'
     from "msdos" to "dosfs", compile and build the executable and
     re-run the above test, '/mnt/rda1' exists (but the file system
     is mounted read-only).

Close #4131
2020-10-12 13:18:27 +02:00

255 lines
6.9 KiB
C

/**
* @file
*
* Block device partition management.
*/
/*
* Copyright (c) 2009, 2020
* embedded brains GmbH
* Dornierstr. 4
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.org/license/LICENSE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rtems/bdpart.h>
#include <rtems/error.h>
#include <rtems/shell.h>
#define RTEMS_BDPART_SHELL_ERROR( fmt, ...) \
do { \
printf( "error: " fmt "\n", ##__VA_ARGS__); \
return -1; \
} while (0)
#define RTEMS_BDPART_SHELL_ERROR_SC( sc, fmt, ...) \
if ((sc) != RTEMS_SUCCESSFUL) { \
printf( "error: " fmt ": %s\n", ##__VA_ARGS__, rtems_status_text( sc)); \
return -1; \
}
typedef enum {
RTEMS_BDPART_SHELL_FS,
RTEMS_BDPART_SHELL_N,
RTEMS_BDPART_SHELL_MBR,
RTEMS_BDPART_SHELL_GPT
} rtems_bdpart_shell_state;
static const char rtems_bdpart_shell_usage [] =
"disk format and utility functions\n"
"\n"
"fdisk DISK_NAME\n"
"\tprints the partition table\n"
"\n"
"fdisk DISK_NAME [FS N1 [N2 ... ]] ... [write] [FORMAT]\n"
"\tcreates a new partition table\n"
"\n"
"fdisk DISK_NAME register\n"
"\tcreates a logical disk for each partition of the disk\n"
"\n"
"fdisk DISK_NAME unregister\n"
"\tdeletes the logical disks associated with the partitions\n"
"\tof the disk\n"
"\n"
"option values:\n"
"\tDISK_NAME: absolute path to disk device like '/dev/hda'\n"
"\tN*: weights of positive integers\n"
"\tFS: 0x00 ... 0xff, fat12, fat16, fat32, data\n"
"\twrite: write the new partition table to the disk\n"
"\tFORMAT: mbr [[no]dos], gpt";
static int rtems_bdpart_shell_main( int argc, char **argv)
{
rtems_status_code sc = RTEMS_SUCCESSFUL;
rtems_bdpart_format format;
rtems_bdpart_partition pt [RTEMS_BDPART_PARTITION_NUMBER_HINT];
unsigned dist [RTEMS_BDPART_PARTITION_NUMBER_HINT];
size_t count = RTEMS_BDPART_PARTITION_NUMBER_HINT;
const char *disk_name = NULL;
bool do_create = false;
bool do_read = false;
bool do_write = false;
bool do_register = false;
bool do_unregister = false;
bool do_dump = false;
if (argc < 2) {
puts( rtems_bdpart_shell_usage);
return -1;
}
disk_name = argv [1];
if (argc == 2) {
do_read = true;
do_dump = true;
} else if (argc == 3) {
/* Check option */
if (strcmp( argv [2], "register") == 0) {
do_read = true;
do_register = true;
} else if (strcmp( argv [2], "unregister") == 0) {
do_read = true;
do_unregister = true;
} else {
RTEMS_BDPART_SHELL_ERROR( "unexpected option: %s", argv [2]);
}
} else {
rtems_bdpart_shell_state state = RTEMS_BDPART_SHELL_FS;
uint8_t current_type = RTEMS_BDPART_MBR_FAT_32;
size_t i = 0;
int ai = 0;
/* Clear partition table */
memset( pt, 0, sizeof( pt));
/* Default format */
format.type = RTEMS_BDPART_FORMAT_MBR;
format.mbr.disk_id = 0;
format.mbr.dos_compatibility = true;
for (ai = 2; ai < argc; ++ai) {
char *s = argv [ai];
unsigned long v = 0;
char *end = NULL;
if (strlen( s) == 0) {
continue;
} else if (strcmp( s, "write") == 0) {
do_write = true;
continue;
} else if (strcmp( s, "mbr") == 0) {
state = RTEMS_BDPART_SHELL_MBR;
format.type = RTEMS_BDPART_FORMAT_MBR;
continue;
} else if (strcmp( s, "gpt") == 0) {
state = RTEMS_BDPART_SHELL_GPT;
format.type = RTEMS_BDPART_FORMAT_GPT;
continue;
}
switch (state) {
case RTEMS_BDPART_SHELL_FS:
v = strtoul( s, &end, 16);
if (*end == '\0') {
if (v <= 0xffU) {
current_type = (uint8_t) v;
} else {
RTEMS_BDPART_SHELL_ERROR( "type value out of range: %s", argv [ai]);
}
} else if (strcmp( s, "fat32") == 0) {
current_type = RTEMS_BDPART_MBR_FAT_32;
} else if (strcmp( s, "data") == 0) {
current_type = RTEMS_BDPART_MBR_DATA;
} else if (strcmp( s, "fat16") == 0) {
current_type = RTEMS_BDPART_MBR_FAT_16;
} else if (strcmp( s, "fat12") == 0) {
current_type = RTEMS_BDPART_MBR_FAT_12;
} else {
RTEMS_BDPART_SHELL_ERROR( "unexpected option: %s", argv [ai]);
}
state = RTEMS_BDPART_SHELL_N;
break;
case RTEMS_BDPART_SHELL_N:
v = strtoul( s, &end, 10);
if (*end == '\0') {
rtems_bdpart_to_partition_type( current_type, pt [i].type);
dist [i] = v;
if (i < count) {
++i;
} else {
RTEMS_BDPART_SHELL_ERROR( "too many partitions");
}
} else {
--ai;
state = RTEMS_BDPART_SHELL_FS;
}
break;
case RTEMS_BDPART_SHELL_MBR:
if (strcmp( s, "dos") == 0) {
format.mbr.dos_compatibility = true;
} else if (strcmp( s, "nodos") == 0) {
format.mbr.dos_compatibility = false;
} else {
RTEMS_BDPART_SHELL_ERROR( "unexpected option: %s", argv [ai]);
}
break;
case RTEMS_BDPART_SHELL_GPT:
RTEMS_BDPART_SHELL_ERROR( "unexpected option: %s", argv [ai]);
default:
RTEMS_BDPART_SHELL_ERROR( "fdisk interal error");
}
}
/* Partition number */
count = i;
/* Actions */
do_create = true;
do_dump = true;
if (do_write) {
do_read = true;
}
}
if (do_create) {
/* Create partitions */
sc = rtems_bdpart_create( disk_name, &format, pt, dist, count);
RTEMS_BDPART_SHELL_ERROR_SC( sc, "cannot create partitions for '%s'", disk_name);
}
if (do_write) {
/* Write partitions */
sc = rtems_bdpart_write( disk_name, &format, pt, count);
RTEMS_BDPART_SHELL_ERROR_SC( sc, "cannot write partitions to '%s'", disk_name);
}
if (do_read) {
/* Read partitions */
count = RTEMS_BDPART_PARTITION_NUMBER_HINT;
sc = rtems_bdpart_read( disk_name, &format, pt, &count);
RTEMS_BDPART_SHELL_ERROR_SC( sc, "cannot read partitions from '%s'", disk_name);
}
if (do_register) {
/* Register partitions */
sc = rtems_bdpart_register( disk_name, pt, count);
RTEMS_BDPART_SHELL_ERROR_SC( sc, "cannot register partitions of '%s'", disk_name);
}
if (do_unregister) {
/* Unregister partitions */
sc = rtems_bdpart_unregister( disk_name, pt, count);
RTEMS_BDPART_SHELL_ERROR_SC( sc, "cannot unregister partitions of '%s'", disk_name);
}
if (do_dump) {
/* Dump partitions */
rtems_bdpart_dump( pt, count);
}
return 0;
}
struct rtems_shell_cmd_tt rtems_shell_FDISK_Command = {
.name = "fdisk",
.usage = rtems_bdpart_shell_usage,
.topic = "files",
.command = rtems_bdpart_shell_main,
.alias = NULL,
.next = NULL
};