#define _EXPOSE_MMC
#if 0
# define DEBUG
#endif
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <inttypes.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <util.h>
#include <time.h>
#include <assert.h>
#include <err.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/cdio.h>
#include <sys/disklabel.h>
#include <sys/dkio.h>
#include <sys/param.h>
#include <fs/udf/udf_mount.h>
#include "mountprog.h"
#include "udf_core.h"
#include "newfs_udf.h"
#define IMPL_NAME "*NetBSD newfs_udf 10.0"
#define APP_VERSION_MAIN 0
#define APP_VERSION_SUB 5
int newfs_udf(int argc, char **argv);
static void usage(void) __attribute__((__noreturn__));
char *format_str;
static int
udf_prepare_format_track512(void)
{
struct mmc_trackinfo ti;
struct mmc_op op;
int error;
if (!(context.format_flags & FORMAT_TRACK512))
return 0;
ti.tracknr = mmc_discinfo.last_track_last_session;
error = udf_update_trackinfo(&ti);
if (error)
return error;
memset(&op, 0, sizeof(op));
op.operation = MMC_OP_RESERVETRACK_NWA;
op.mmc_profile = mmc_discinfo.mmc_profile;
op.extent = 512;
error = ioctl(dev_fd, MMCOP, &op);
return error;
}
static int
udf_do_newfs(void)
{
int error;
error = udf_do_newfs_prefix();
if (error)
return error;
error = udf_do_rootdir();
if (error)
return error;
error = udf_do_newfs_postfix();
return error;
}
static void
usage(void)
{
(void)fprintf(stderr, "Usage: %s [-cFM] [-L loglabel] "
"[-P discid] [-S sectorsize] [-s size] [-p perc] "
"[-t gmtoff] [-v min_udf] [-V max_udf] special\n", getprogname());
exit(EXIT_FAILURE);
}
int
main(int argc, char **argv)
{
struct tm *tm;
time_t now;
off_t setsize;
char scrap[255], *colon;
int ch, req_enable, req_disable;
int error;
setprogname(argv[0]);
format_str = strdup("");
req_enable = req_disable = 0;
setsize = 0;
emul_mmc_profile = -1;
emul_packetsize = 1;
emul_sectorsize = 512;
emul_size = 0;
srandom((unsigned long) time(NULL));
udf_init_create_context();
context.app_name = "*NetBSD UDF";
context.app_version_main = APP_VERSION_MAIN;
context.app_version_sub = APP_VERSION_SUB;
context.impl_name = IMPL_NAME;
context.min_udf = 0x201;
context.max_udf = 0x250;
(void)time(&now);
tm = localtime(&now);
context.gmtoff = tm->tm_gmtoff;
while ((ch = getopt(argc, argv, "cFL:Mp:P:s:S:B:t:v:V:")) != -1) {
switch (ch) {
case 'c' :
context.check_surface = 1;
break;
case 'F' :
context.create_new_session = 1;
break;
case 'L' :
if (context.logvol_name) free(context.logvol_name);
context.logvol_name = strdup(optarg);
break;
case 'M' :
req_disable |= FORMAT_META;
break;
case 'p' :
context.meta_perc = a_num(optarg, "meta_perc");
context.meta_perc = MIN(context.meta_perc, 99);
context.meta_perc = MAX(context.meta_perc, 1);
break;
case 'v' :
context.min_udf = a_udf_version(optarg, "min_udf");
if (context.min_udf > context.max_udf)
context.max_udf = context.min_udf;
break;
case 'V' :
context.max_udf = a_udf_version(optarg, "max_udf");
if (context.min_udf > context.max_udf)
context.min_udf = context.max_udf;
break;
case 'P' :
if ((colon = strstr(optarg, ":"))) {
if (context.volset_name)
free(context.volset_name);
*colon = 0;
context.volset_name = strdup(optarg);
optarg = colon+1;
}
if (context.primary_name)
free(context.primary_name);
if ((strstr(optarg, ":")))
errx(1, "primary name can't have ':' in its name");
context.primary_name = strdup(optarg);
break;
case 's' :
if (dehumanize_number(optarg, &setsize) < 0)
errx(1, "can't parse size argument");
setsize = MAX(0, setsize);
break;
case 'S' :
emul_sectorsize = a_num(optarg, "secsize");
emul_sectorsize = MAX(512, emul_sectorsize);
break;
case 'B' :
emul_packetsize = a_num(optarg,
"blockingnr, packetsize");
emul_packetsize = MAX(emul_packetsize, 1);
emul_packetsize = MIN(emul_packetsize, 32);
break;
case 't' :
context.gmtoff = a_num(optarg, "gmtoff");
break;
default :
usage();
}
}
if (optind + 1 != argc)
usage();
dev_name = argv[optind];
emul_size = setsize;
error = udf_opendisc(dev_name, O_RDWR | O_CREAT | O_TRUNC);
if (error) {
udf_closedisc();
errx(1, "can't open device");
}
error = udf_update_discinfo();
if (error) {
udf_closedisc();
errx(1, "can't retrieve discinfo");
}
error = udf_proces_names();
if (error) {
udf_closedisc();
errx(1, "bad names given");
}
error = udf_derive_format(req_enable, req_disable);
if (error) {
udf_closedisc();
errx(1, "can't derive format from media/settings");
}
udf_dump_discinfo(&mmc_discinfo);
printf("Formatting disc compatible with UDF version %x to %x\n\n",
context.min_udf, context.max_udf);
(void)snprintb(scrap, sizeof(scrap), FORMAT_FLAGBITS,
(uint64_t) context.format_flags);
printf("UDF properties %s\n", scrap);
printf("Volume set `%s'\n", context.volset_name);
printf("Primary volume `%s`\n", context.primary_name);
printf("Logical volume `%s`\n", context.logvol_name);
if (context.format_flags & FORMAT_META)
printf("Metadata percentage %d %%\n", context.meta_perc);
printf("\n");
error = udf_prepare_disc();
if (error) {
udf_closedisc();
errx(1, "preparing disc failed");
}
error = udf_prepare_format_track512();
if (error) {
udf_closedisc();
errx(1, "reservation for track512 failed");
}
udf_allow_writing();
error = udf_do_newfs();
udf_closedisc();
if (error)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}