udftools

- Main Page
- Classes
- Files

- File List
- File Members

All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
main.c
Go to the documentation of this file.
1 /*
2 * main.c
3 *
4 * Copyright (c) 2002 Ben Fennema <bfennema@falcon.csc.calpoly.edu>
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23 #include "config.h"
24
25 #include <malloc.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <time.h>
33 #include <sys/time.h>
34 #include <errno.h>
35
36 #include "cdrwtool.h"
37 #include "options.h"
38
39 #include "../mkudffs/defaults.h"
40 #include "../mkudffs/mkudffs.h"
41
42 int write_func(struct udf_disc *disc, struct udf_extent *ext)
43 {
44 static unsigned char *buffer = NULL;
45 static size_t bufferlen = 0;
46 static int lastpacket = -1;
47 int fd = *(int *)disc->write_data;
48 size_t offset;
49 int packet;
50 struct udf_desc *desc;
51 struct udf_data *data;
52
53 if (buffer == NULL)
54 {
55 bufferlen = disc->blocksize * 32;
56 buffer = calloc(bufferlen, 1);
57 }
58
59 if (ext == NULL)
60 {
61 if (lastpacket != -1)
62 {
63 if (write_blocks(fd, buffer, lastpacket * 32, 32) < 0)
64 return -1;
65 }
66 }
67 else if (!(ext->space_type & (USPACE|RESERVED)))
68 {
69 desc = ext->head;
70 while (desc != NULL)
71 {
72 packet = (uint64_t)(ext->start + desc->offset) / 32;
73 if (lastpacket != -1 && packet != lastpacket)
74 {
75 if (write_blocks(fd, buffer, lastpacket * 32, 32) < 0)
76 return -1;
77 memset(buffer, 0x00, bufferlen);
78 }
79 data = desc->data;
80 offset = ((uint64_t)(ext->start + desc->offset) - (packet * 32)) << disc->blocksize_bits;
81 while (data != NULL)
82 {
83 if (data->length + offset > bufferlen)
84 {
85 memcpy(buffer + offset, data->buffer, bufferlen - offset);
86 if (write_blocks(fd, buffer, packet * 32, 32) < 0)
87 return -1;
88 memset(buffer, 0x00, bufferlen);
89 lastpacket = ++ packet;
90
91 memcpy(buffer, data->buffer + (bufferlen - offset), data->length - (bufferlen - offset));
92 offset = data->length - (bufferlen - offset);
93 }
94 else
95 {
96 lastpacket = packet;
97 memcpy(buffer + offset, data->buffer, data->length);
98 offset += data->length;
99 }
100
101 if (offset == disc->blocksize * 32)
102 {
103 if (write_blocks(fd, buffer, packet * 32, 32) < 0)
104 return -1;
105 memset(buffer, 0x00, bufferlen);
106 lastpacket = -1;
107 offset = 0;
108 packet ++;
109 }
110 data = data->next;
111 }
112 desc = desc->next;
113 }
114 }
115 return 0;
116 }
117
118 int quick_setup(int fd, struct cdrw_disc *disc, char *device)
119 {
120 disc_info_t di;
121 track_info_t ti;
122 int ret, i;
123 unsigned blocks;
124
125 printf("Settings for %s:\n", device);
126 printf("\t%s packets, size %u\n", disc->fpacket ? "Fixed" : "Variable",
127 disc->packet_size);
128 printf("\tMode-%d disc\n", disc->write_type);
129
130 printf( "\nI'm going to do a quick setup of %s. The disc is going to "
131 "be blanked and formatted with one big track. All data on "
132 "the device will be lost!! Press CTRL-C to cancel now.\n"
133 "ENTER to continue.\n",
134 device);
135
136 getchar();
137
138 printf("Initiating quick disc blank\n");
139
140 if ((ret = blank_disc(fd, BLANK_FAST)))
141 return ret;
142
143 if ((ret = read_disc_info(fd, &di)) < 0)
144 return ret;
145
146 if ((ret = read_track_info(fd, &ti, 1)) < 0)
147 return ret;
148
149 blocks = msf_to_lba(di.lead_out_m, di.lead_out_s, di.lead_out_f) - 152;
150 if (disc->fpacket)
151 {
152 /* fixed packets format usable blocks */
153 blocks = ((blocks + 7) / (disc->packet_size + 7)) * disc->packet_size;
154 }
155 printf("Disc capacity is %u blocks (%uKB/%uMB)\n",
156 blocks, blocks * 2, blocks / 512);
157
158 if (!disc->offset)
159 disc->udf_disc.head->blocks = blocks;
160 else
161 disc->udf_disc.head->blocks = disc->offset;
162
163 if (disc->fpacket)
164 {
165 if (!disc->offset)
166 disc->offset = blocks;
167 printf("Formatting track\n");
168 if ((ret = format_disc(fd, disc)))
169 return ret;
170 add_type2_sparable_partition(&disc->udf_disc, 0, 2, 0);
171 disc->udf_disc.udf_pd[0]->accessType = cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE);
172 }
173 else
174 {
175 disc->reserve_track = blocks;
176 printf("Reserving track\n");
177 if ((ret = reserve_track(fd, disc)))
178 return ret;
179 }
180
181 disc->udf_disc.flags |= FLAG_UNALLOC_BITMAP;
182 for (i=0; i<UDF_ALLOC_TYPE_SIZE; i++)
183 {
184 if (disc->udf_disc.sizing[i].denomSize == 0)
185 memcpy(&disc->udf_disc.sizing[i], &default_sizing[DEFAULT_CDRW][i], sizeof(default_sizing[DEFAULT_CDRW][i]));
186 }
187
188 split_space(&disc->udf_disc);
189
190 setup_vrs(&disc->udf_disc);
191 setup_anchor(&disc->udf_disc);
192 setup_partition(&disc->udf_disc);
193 setup_vds(&disc->udf_disc);
194
195 dump_space(&disc->udf_disc);
196
197 printf("Writing UDF structures to disc\n");
198 write_disc(&disc->udf_disc);
199 disc->udf_disc.write(&disc->udf_disc, NULL);
200 sync_cache(fd);
201
202 printf("Quick setup complete!\n");
203 return 0;
204 }
205
206 int mkudf_session(int fd, struct cdrw_disc *disc)
207 {
208 int i;
209
210 disc->udf_disc.head->blocks = disc->offset;
211 add_type2_sparable_partition(&disc->udf_disc, 0, 2, 0);
212 disc->udf_disc.udf_pd[0]->accessType = cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE);
213
214 disc->udf_disc.flags |= FLAG_UNALLOC_BITMAP;
215 for (i=0; i<UDF_ALLOC_TYPE_SIZE; i++)
216 {
217 if (disc->udf_disc.sizing[i].denomSize == 0)
218 memcpy(&disc->udf_disc.sizing[i], &default_sizing[DEFAULT_CDRW][i], sizeof(default_sizing[DEFAULT_CDRW][i]));
219 }
220
221 split_space(&disc->udf_disc);
222
223 setup_vrs(&disc->udf_disc);
224 setup_anchor(&disc->udf_disc);
225 setup_partition(&disc->udf_disc);
226 setup_vds(&disc->udf_disc);
227
228 dump_space(&disc->udf_disc);
229
230 printf("Writing UDF structures to disc\n");
231 write_disc(&disc->udf_disc);
232 disc->udf_disc.write(&disc->udf_disc, NULL);
233 sync_cache(fd);
234 return 0;
235 }
236
237 int main(int argc, char *argv[])
238 {
239 struct cdrw_disc disc;
240 write_params_t w;
241 char filename[NAME_MAX];
242 int fd, ret;
243
244 memset(&disc, 0x00, sizeof(disc));
245 cdrw_init_disc(&disc);
246 udf_init_disc(&disc.udf_disc);
247 strcpy(filename, CDROM_DEVICE);
248 parse_args(argc, argv, &disc, filename);
249 if (((fd = open(filename, O_RDWR | O_NONBLOCK)) < 0) &&
250 ((errno != EROFS) ||
251 ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0)))
252 {
253 perror("open cdrom device");
254 return fd;
255 }
256 disc.udf_disc.write = write_func;
257 disc.udf_disc.write_data = &fd;
258
259 if ((ret = cdrom_open_check(fd)))
260 {
261 fprintf(stderr, "set_options\n");
262 cdrom_close(fd);
263 return ret;
264 }
265
266 if ((ret = read_buffer_cap(fd, &disc)))
267 {
268 cdrom_close(fd);
269 return ret;
270 }
271
272 if ((ret = get_write_mode(fd, &w)))
273 {
274 fprintf(stderr, "get_write\n");
275 cdrom_close(fd);
276 return ret;
277 }
278
279 if ((ret = set_cd_speed(fd, disc.speed)))
280 {
281 fprintf(stderr, "set speed\n");
282 cdrom_close(fd);
283 return ret;
284 }
285
286 if (disc.get_settings || disc.disc_track_info)
287 {
288 if (disc.get_settings)
289 print_params(&w);
290 if (disc.disc_track_info)
291 print_disc_track_info(fd);
292 cdrom_close(fd);
293 return ret;
294 }
295
296 /* define write parameters based on command line options */
297 make_write_page(&w, &disc);
298
299 if ((ret = set_write_mode(fd, &w)))
300 {
301 printf("set_write\n");
302 cdrom_close(fd);
303 return ret;
304 }
305
306 if (disc.close_track)
307 {
308 ret = close_track(fd, disc.close_track);
309 cdrom_close(fd);
310 return ret;
311 }
312
313 if (disc.close_session)
314 {
315 ret = close_session(fd, disc.close_session);
316 cdrom_close(fd);
317 return ret;
318 }
319
320 if (disc.quick_setup)
321 {
322 ret = quick_setup(fd, &disc, filename);
323 cdrom_close(fd);
324 return ret;
325 }
326
327 if (disc.blank)
328 {
329 ret = blank_disc(fd, disc.blank);
330 cdrom_close(fd);
331 return ret;
332 }
333
334 if (disc.format)
335 {
336 ret = format_disc(fd, &disc);
337 cdrom_close(fd);
338 return ret;
339 }
340
341 if (disc.mkudf)
342 {
343 ret = mkudf_session(fd, &disc);
344 cdrom_close(fd);
345 return ret;
346 }
347
348 if (disc.filename[0] != '\0')
349 {
350 ret = write_file(fd, &disc);
351 cdrom_close(fd);
352 return ret;
353 }
354
355 if (disc.reserve_track)
356 {
357 ret = reserve_track(fd, &disc);
358 cdrom_close(fd);
359 return ret;
360 }
361
362 cdrom_close(fd);
363 return 0;
364 }
device
int device
Definition: wrudf.c:18
udf_disc::write_data
void * write_data
Definition: libudffs.h:108
udf_extent::blocks
uint32_t blocks
Definition: libudffs.h:135
PD_ACCESS_TYPE_REWRITABLE
#define PD_ACCESS_TYPE_REWRITABLE
Definition: ecma_167.h:292
write_blocks
int write_blocks(int fd, unsigned char *buffer, int lba, int blocks)
Definition: cdrwtool.c:302
disc_info::lead_out_m
uint8_t lead_out_m
Definition: cdrwtool.h:154
cpu_to_le32
#define cpu_to_le32(x)
Definition: bswap.h:247
cdrw_disc::get_settings
unsigned char get_settings
Definition: cdrwtool.h:78
close_track
int close_track(int fd, unsigned int track)
Definition: cdrwtool.c:528
udf_disc::blocksize
uint16_t blocksize
Definition: libudffs.h:98
read_track_info
int read_track_info(int fd, track_info_t *ti, int trackno)
Definition: cdrwtool.c:486
main
int main(int argc, char *argv[])
Definition: main.c:237
cdrw_disc
Definition: cdrwtool.h:74
udf_disc::udf_pd
struct partitionDesc * udf_pd[2]
Definition: libudffs.h:114
USPACE
Definition: libudffs.h:72
options.h
cdrw_disc::udf_disc
struct udf_disc udf_disc
Definition: cdrwtool.h:95
quick_setup
int quick_setup(int fd, struct cdrw_disc *disc, char *device)
Definition: main.c:118
NAME_MAX
#define NAME_MAX
Definition: cdrwtool.h:57
setup_vrs
void setup_vrs(struct udf_disc *disc)
Definition: mkudffs.c:367
cdrw_disc::quick_setup
unsigned char quick_setup
Definition: cdrwtool.h:91
FLAG_UNALLOC_BITMAP
#define FLAG_UNALLOC_BITMAP
Definition: libudffs.h:37
udf_disc::head
struct udf_extent * head
Definition: libudffs.h:127
RESERVED
Definition: libudffs.h:63
cdrw_disc::reserve_track
unsigned int reserve_track
Definition: cdrwtool.h:90
setup_partition
void setup_partition(struct udf_disc *disc)
Definition: mkudffs.c:449
cdrw_disc::close_session
unsigned int close_session
Definition: cdrwtool.h:94
read_disc_info
int read_disc_info(int fd, disc_info_t *di)
Definition: cdrwtool.c:456
read_buffer_cap
int read_buffer_cap(int fd, struct cdrw_disc *disc)
Definition: cdrwtool.c:570
cdrw_disc::offset
unsigned long offset
Definition: cdrwtool.h:77
msf_to_lba
int msf_to_lba(int m, int s, int f)
Definition: cdrwtool.c:44
cdrom_open_check
int cdrom_open_check(int fd)
Definition: cdrwtool.c:623
dump_space
void dump_space(struct udf_disc *disc)
Definition: mkudffs.c:331
cdrw_disc::close_track
unsigned int close_track
Definition: cdrwtool.h:93
udf_init_disc
void udf_init_disc(struct udf_disc *disc)
Definition: mkudffs.c:46
cdrw_disc::blank
unsigned char blank
Definition: cdrwtool.h:80
cdrw_disc::packet_size
unsigned char packet_size
Definition: cdrwtool.h:82
cdrw_disc::speed
unsigned char speed
Definition: cdrwtool.h:88
print_disc_track_info
int print_disc_track_info(int fd)
Definition: cdrwtool.c:706
cdrw_init_disc
void cdrw_init_disc(struct cdrw_disc *disc)
Definition: cdrwtool.c:769
disc_info
Definition: cdrwtool.h:114
disc_info::lead_out_f
uint8_t lead_out_f
Definition: cdrwtool.h:156
cdrw_disc::filename
char filename[NAME_MAX]
Definition: cdrwtool.h:76
cdrw_disc::write_type
unsigned char write_type
Definition: cdrwtool.h:84
blank_disc
int blank_disc(int fd, int type)
Definition: cdrwtool.c:392
mkudf_session
int mkudf_session(int fd, struct cdrw_disc *disc)
Definition: main.c:206
write_disc
int write_disc(struct udf_disc *disc)
Definition: mkudffs.c:351
close_session
int close_session(int fd, unsigned int track)
Definition: cdrwtool.c:549
udf_data
Definition: libudffs.h:155
udf_sizing::denomSize
uint32_t denomSize
Definition: libudffs.h:81
reserve_track
int reserve_track(int fd, struct cdrw_disc *disc)
Definition: cdrwtool.c:507
udf_disc::sizing
struct udf_sizing sizing[UDF_ALLOC_TYPE_SIZE]
Definition: libudffs.h:105
cdrw_disc::format
unsigned char format
Definition: cdrwtool.h:86
split_space
void split_space(struct udf_disc *disc)
Definition: mkudffs.c:198
print_params
void print_params(write_params_t *wp)
Definition: cdrwtool.c:759
format_disc
int format_disc(int fd, struct cdrw_disc *disc)
Definition: cdrwtool.c:412
BLANK_FAST
#define BLANK_FAST
Definition: cdrwtool.h:47
setup_anchor
void setup_anchor(struct udf_disc *disc)
Definition: mkudffs.c:402
DEFAULT_CDRW
#define DEFAULT_CDRW
Definition: mkudffs.h:39
udf_extent
Definition: libudffs.h:131
write_params_t
Definition: cdrwtool.h:62
cdrw_disc::disc_track_info
unsigned char disc_track_info
Definition: cdrwtool.h:85
sync_cache
int sync_cache(int fd)
Definition: cdrwtool.c:292
cdrw_disc::fpacket
unsigned char fpacket
Definition: cdrwtool.h:81
udf_extent::start
uint32_t start
Definition: libudffs.h:134
default_sizing
struct udf_sizing default_sizing[][UDF_ALLOC_TYPE_SIZE]
Definition: defaults.c:32
cdrom_close
void cdrom_close(int fd)
Definition: cdrwtool.c:614
udf_disc::flags
uint32_t flags
Definition: libudffs.h:100
parse_args
void parse_args(int argc, char *argv[], struct cdrw_disc *disc, char *device)
Definition: options.c:69
udf_desc
Definition: libudffs.h:144
udf_disc
Definition: libudffs.h:95
udf_extent::head
struct udf_desc * head
Definition: libudffs.h:137
make_write_page
void make_write_page(write_params_t *w, struct cdrw_disc *disc)
Definition: cdrwtool.c:734
partitionDesc::accessType
uint32_t accessType
Definition: ecma_167.h:268
get_write_mode
int get_write_mode(int fd, write_params_t *w)
Definition: cdrwtool.c:242
set_write_mode
int set_write_mode(int fd, write_params_t *w)
Definition: cdrwtool.c:176
disc_info::lead_out_s
uint8_t lead_out_s
Definition: cdrwtool.h:155
set_cd_speed
int set_cd_speed(int fd, int speed)
Definition: cdrwtool.c:594
add_type2_sparable_partition
void add_type2_sparable_partition(struct udf_disc *disc, uint16_t partitionNum, uint8_t spartable, uint16_t packetlen)
Definition: mkudffs.c:1052
write_func
int write_func(struct udf_disc *disc, struct udf_extent *ext)
Definition: main.c:42
udf_disc::blocksize_bits
uint8_t blocksize_bits
Definition: libudffs.h:99
setup_vds
void setup_vds(struct udf_disc *disc)
Definition: mkudffs.c:758
UDF_ALLOC_TYPE_SIZE
Definition: libudffs.h:92
track_info
Definition: cdrwtool.h:162
udf_disc::write
int(* write)(struct udf_disc *, struct udf_extent *)
Definition: libudffs.h:107
cdrwtool.h
udf_extent::space_type
enum udf_space_type space_type
Definition: libudffs.h:133
write_file
int write_file(int fd, struct cdrw_disc *disc)
Definition: cdrwtool.c:323
cdrw_disc::mkudf
unsigned char mkudf
Definition: cdrwtool.h:92
CDROM_DEVICE
#define CDROM_DEVICE
Definition: cdrwtool.h:20
ti
struct cdrom_trackinfo ti
Definition: wrudf-cdrw.c:50