~ubuntu-branches/ubuntu/raring/vice/raring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * vdrive-internal.c - Virtual disk-drive implementation.
 *
 * Written by
 *  Andreas Boose <viceteam@t-online.de>
 *
 * This file is part of VICE, the Versatile Commodore Emulator.
 * See README for copyright notice.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307  USA.
 *
 */

#include "vice.h"

#include <stdio.h>
#include <stdlib.h>

#include "attach.h"
#include "cbmdos.h"
#include "cbmimage.h"
#include "diskimage.h"
#include "lib.h"
#include "log.h"
#include "machine-drive.h"
#include "types.h"
#include "vdrive-command.h"
#include "vdrive-internal.h"
#include "vdrive.h"


static log_t vdrive_internal_log = LOG_DEFAULT;


static vdrive_t *open_fsimage(const char *name, unsigned int read_only)
{
    vdrive_t *vdrive;
    disk_image_t *image;

    image = (disk_image_t *)lib_malloc(sizeof(disk_image_t));

    image->gcr = NULL;
    image->read_only = read_only;

    image->device = DISK_IMAGE_DEVICE_FS;

    disk_image_media_create(image);

    disk_image_name_set(image, lib_stralloc(name));

    if (disk_image_open(image) < 0) {
        disk_image_media_destroy(image);
        lib_free(image);
        log_error(vdrive_internal_log, "Cannot open file `%s'", name);
        return NULL;
    }

    vdrive = (vdrive_t *)lib_calloc(1, sizeof(vdrive_t));

    vdrive_device_setup(vdrive, 100);
    vdrive->image = image;
    vdrive_attach_image(image, 100, vdrive);
    return vdrive;
}

static vdrive_t *open_rawimage(unsigned int unit, unsigned int read_only)
{
    vdrive_t *vdrive;

    vdrive = file_system_get_vdrive(unit);

    return vdrive;
}

vdrive_t *vdrive_internal_open_disk_image(const char *name,
                                          unsigned int unit,
                                          unsigned int read_only)
{
    machine_drive_flush();

    switch (unit) {
      case 8:
      case 9:
      case 10:
      case 11:
        return open_rawimage(unit, read_only);
      default:
        break;
    }

    return open_fsimage(name, read_only);
}

static int close_fsimage(vdrive_t *vdrive)
{
    disk_image_t *image;

    image = vdrive->image;

    vdrive_detach_image(image, 100, vdrive);

    if (disk_image_close(image) < 0)
        return -1;

    disk_image_media_destroy(image);
    lib_free(image);
    lib_free(vdrive);

    return 0;
}

static int close_rawimage(vdrive_t *vdrive)
{
    return 0;
}

int vdrive_internal_close_disk_image(vdrive_t *vdrive)
{
    switch (vdrive->unit) {
      case 8:
      case 9:
      case 10:
      case 11:
        return close_rawimage(vdrive);
      default:
        break;
    }

    return close_fsimage(vdrive);
}

static int vdrive_internal_format_disk_image(const char *filename,
                                             const char *disk_name)
{
    vdrive_t *vdrive;
    const char *format_name;
    int status = 0;

    format_name = (disk_name == NULL) ? " " : disk_name;

    /* FIXME: Pass unit here.  */
    vdrive = vdrive_internal_open_disk_image(filename, 0, 0);

    if (vdrive == NULL)
        return -1;

    if (vdrive_command_format(vdrive, format_name) != CBMDOS_IPE_OK)
        status = -1;

    if (vdrive_internal_close_disk_image(vdrive) < 0)
        return -1;

    return status;
}

int vdrive_internal_create_format_disk_image(const char *filename,
                                             const char *diskname,
                                             unsigned int type)
{
    if (cbmimage_create_image(filename, type) < 0)
        return -1;
    if (vdrive_internal_format_disk_image(filename, diskname) < 0)
        return -1;

    return 0;
}

void vdrive_internal_init(void)
{
    vdrive_internal_log = log_open("VDrive Internal");
}