Add -co option ("copy out").
Add filesys_done() for closing up filesystem shop.
Add -q option for powering off.
PANIC ("can't open root dir file");
}
+/* Shuts down the filesystem module, writing any unwritten data
+ to disk.
+ Currently there's nothing to do. You'll need to add code here
+ when you implement write-behind caching. */
+void
+filesys_done (void)
+{
+}
+
/* Creates a file named NAME with the given INITIAL_SIZE.
Returns true if successful, false otherwise.
Fails if a file named NAME already exists,
extern struct file *free_map_file;
void filesys_init (bool format);
+void filesys_done (void);
bool filesys_create (const char *name, off_t initial_size);
struct file *filesys_open (const char *name);
bool filesys_remove (const char *name);
#include "threads/mmu.h"
#include "threads/palloc.h"
-/* Filename and file size to use for copy operations,
- as "filename:size". */
-char *fsutil_copy_arg;
+/* Destination filename and size for copy-in operations. */
+char *fsutil_copyin_file;
+int fsutil_copyin_size;
+
+/* Source filename for copy-out operations. */
+char *fsutil_copyout_file;
/* Name of a file print to print to console. */
char *fsutil_print_file;
to a file named FILENAME in the filesystem.
The file will be SIZE bytes in length. */
static void
-copy (const char *filename, off_t size)
+copy_in (const char *filename, off_t size)
{
struct disk *src;
struct file *dst;
file_close (dst);
}
+/* Copies FILENAME from the file system to the scratch disk.
+ The first four bytes of the first sector in the disk
+ receive the file's size in bytes as a little-endian integer.
+ The second and subsequent sectors receive the file's data. */
+static void
+copy_out (const char *filename)
+{
+ void *buffer;
+ struct file *src;
+ struct disk *dst;
+ off_t size;
+ disk_sector_t sector;
+
+ buffer = palloc_get (PAL_ASSERT | PAL_ZERO);
+
+ /* Open source file. */
+ src = filesys_open (filename);
+ if (src == NULL)
+ PANIC ("%s: open failed", filename);
+ size = file_length (src);
+
+ /* Open target disk. */
+ dst = disk_get (1, 0);
+ if (dst == NULL)
+ PANIC ("couldn't open target disk (hdc or hd1:0)");
+ if (size + DISK_SECTOR_SIZE > (off_t) disk_size (dst) * DISK_SECTOR_SIZE)
+ PANIC ("target disk is too small for %lld-byte file",
+ (unsigned long long) size);
+
+ /* Write size to sector 0. */
+ *(uint32_t *) buffer = size;
+ disk_write (dst, 0, buffer);
+
+ /* Do copy. */
+ sector = 1;
+ while (size > 0)
+ {
+ int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
+ if (file_read (src, buffer, chunk_size) != chunk_size)
+ PANIC ("%s: read failed with %lld bytes unread",
+ filename, (unsigned long long) size);
+ disk_write (dst, sector++, buffer);
+ size -= chunk_size;
+ }
+ palloc_free (buffer);
+
+ file_close (src);
+}
+
/* Executes the filesystem operations described by the variables
declared in fsutil.h. */
void
fsutil_run (void)
{
- if (fsutil_copy_arg != NULL)
- {
- char *save;
- char *filename = strtok_r (fsutil_copy_arg, ":", &save);
- char *size = strtok_r (NULL, "", &save);
-
- if (filename == NULL || size == NULL)
- PANIC ("bad format for -cp option; use -u for usage");
+ if (fsutil_copyin_file != NULL)
+ copy_in (fsutil_copyin_file, fsutil_copyin_size);
- copy (filename, atoi (size));
- }
+ if (fsutil_copyout_file != NULL)
+ copy_out (fsutil_copyout_file);
if (fsutil_print_file != NULL)
fsutil_print (fsutil_print_file);
#include <stdbool.h>
-extern char *fsutil_copy_arg;
+extern char *fsutil_copyin_file;
+extern int fsutil_copyin_size;
+extern char *fsutil_copyout_file;
extern char *fsutil_print_file;
extern char *fsutil_remove_file;
extern bool fsutil_list_files;
static char *initial_program;
#endif
+/* Power off after running requested actions? */
+static bool power_off;
+
static void ram_init (void);
static void argv_init (void);
+static void do_power_off (void);
int main (void) NO_RETURN;
test ();
#endif
+ if (power_off)
+ do_power_off ();
+
/* Terminate this thread. */
thread_exit ();
}
argv_init (void)
{
char *cmd_line, *pos;
- char *argv[LOADER_CMD_LINE_LEN / 2 + 1];
+ char *argv[LOADER_CMD_LINE_LEN / 2 + 2];
int argc = 0;
int i;
pos = strchr (pos, '\0') + 1;
}
argv[argc] = "";
+ argv[argc + 1] = "";
/* Parse the words. */
for (i = 0; i < argc; i++)
random_init (atoi (argv[++i]));
else if (!strcmp (argv[i], "-d"))
debug_enable (argv[++i]);
+ else if (!strcmp (argv[i], "-q"))
+ power_off = true;
#ifdef USERPROG
else if (!strcmp (argv[i], "-ex"))
initial_program = argv[++i];
#ifdef FILESYS
else if (!strcmp (argv[i], "-f"))
format_filesys = true;
- else if (!strcmp (argv[i], "-cp"))
- fsutil_copy_arg = argv[++i];
+ else if (!strcmp (argv[i], "-ci"))
+ {
+ fsutil_copyin_file = argv[++i];
+ fsutil_copyin_size = atoi (argv[++i]);
+ }
+ else if (!strcmp (argv[i], "-co"))
+ fsutil_copyout_file = argv[++i];
else if (!strcmp (argv[i], "-p"))
fsutil_print_file = argv[++i];
else if (!strcmp (argv[i], "-r"))
#endif
#ifdef FILESYS
" -f Format the filesystem disk (hdb or hd0:1).\n"
- " -cp FILENAME:SIZE Copy SIZE bytes from the scratch disk (hdc\n"
+ " -ci FILENAME SIZE Copy SIZE bytes from the scratch disk (hdc\n"
" or hd1:0) into the filesystem as FILENAME\n"
+ " -co FILENAME Copy FILENAME to the scratch disk, with\n"
+ " size at start of sector 0 and data afterward\n"
" -p FILENAME Print the contents of FILENAME\n"
" -r FILENAME Delete FILENAME\n"
" -ls List the files in the filesystem\n"
" -D Dump complete filesystem contents\n"
#endif
+ " -q Power off after doing requested actions.\n"
);
}
else
PANIC ("unknown option `%s'", argv[i]);
}
+
+void
+do_power_off (void)
+{
+ const char s[] = "Shutdown";
+ const char *p;
+
+#ifdef FILESYS
+ filesys_done ();
+#endif
+
+ printf ("Powering off...\n");
+ for (p = s; *p != '\0'; p++)
+ outb (0x8900, *p);
+ for (;;);
+}