xref: /minix3/usr.sbin/makefs/cd9660/cd9660_archimedes.c (revision 9f988b79349f9b89ecc822458c30ec8897558560)
1*9f988b79SJean-Baptiste Boric /* $NetBSD: cd9660_archimedes.c,v 1.2 2013/01/28 21:03:28 christos Exp $ */
2*9f988b79SJean-Baptiste Boric 
3*9f988b79SJean-Baptiste Boric /*-
4*9f988b79SJean-Baptiste Boric  * Copyright (c) 1998, 2009 Ben Harris
5*9f988b79SJean-Baptiste Boric  * All rights reserved.
6*9f988b79SJean-Baptiste Boric  *
7*9f988b79SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or without
8*9f988b79SJean-Baptiste Boric  * modification, are permitted provided that the following conditions
9*9f988b79SJean-Baptiste Boric  * are met:
10*9f988b79SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
11*9f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
12*9f988b79SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above copyright
13*9f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer in the
14*9f988b79SJean-Baptiste Boric  *    documentation and/or other materials provided with the distribution.
15*9f988b79SJean-Baptiste Boric  * 3. The name of the author may not be used to endorse or promote products
16*9f988b79SJean-Baptiste Boric  *    derived from this software without specific prior written permission.
17*9f988b79SJean-Baptiste Boric  *
18*9f988b79SJean-Baptiste Boric  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*9f988b79SJean-Baptiste Boric  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*9f988b79SJean-Baptiste Boric  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21*9f988b79SJean-Baptiste Boric  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22*9f988b79SJean-Baptiste Boric  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23*9f988b79SJean-Baptiste Boric  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*9f988b79SJean-Baptiste Boric  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*9f988b79SJean-Baptiste Boric  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*9f988b79SJean-Baptiste Boric  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27*9f988b79SJean-Baptiste Boric  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*9f988b79SJean-Baptiste Boric  */
29*9f988b79SJean-Baptiste Boric /*
30*9f988b79SJean-Baptiste Boric  * cd9660_archimedes.c - support for RISC OS "ARCHIMEDES" extension
31*9f988b79SJean-Baptiste Boric  *
32*9f988b79SJean-Baptiste Boric  * RISC OS CDFS looks for a special block at the end of the System Use
33*9f988b79SJean-Baptiste Boric  * Field for each file.  If present, this contains the RISC OS load
34*9f988b79SJean-Baptiste Boric  * and exec address (used to hold the file timestamp and type), the
35*9f988b79SJean-Baptiste Boric  * file attributes, and a flag indicating whether the first character
36*9f988b79SJean-Baptiste Boric  * of the filename should be replaced with '!' (since many special
37*9f988b79SJean-Baptiste Boric  * RISC OS filenames do).
38*9f988b79SJean-Baptiste Boric  */
39*9f988b79SJean-Baptiste Boric 
40*9f988b79SJean-Baptiste Boric #if HAVE_NBTOOL_CONFIG_H
41*9f988b79SJean-Baptiste Boric #include "nbtool_config.h"
42*9f988b79SJean-Baptiste Boric #endif
43*9f988b79SJean-Baptiste Boric 
44*9f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
45*9f988b79SJean-Baptiste Boric #if defined(__RCSID) && !defined(__lint)
46*9f988b79SJean-Baptiste Boric __RCSID("$NetBSD: cd9660_archimedes.c,v 1.2 2013/01/28 21:03:28 christos Exp $");
47*9f988b79SJean-Baptiste Boric #endif  /* !__lint */
48*9f988b79SJean-Baptiste Boric 
49*9f988b79SJean-Baptiste Boric #include <assert.h>
50*9f988b79SJean-Baptiste Boric #include <stdint.h>
51*9f988b79SJean-Baptiste Boric #include <stdio.h>
52*9f988b79SJean-Baptiste Boric #include <string.h>
53*9f988b79SJean-Baptiste Boric #include <util.h>
54*9f988b79SJean-Baptiste Boric 
55*9f988b79SJean-Baptiste Boric #include "makefs.h"
56*9f988b79SJean-Baptiste Boric #include "cd9660.h"
57*9f988b79SJean-Baptiste Boric #include "cd9660_archimedes.h"
58*9f988b79SJean-Baptiste Boric 
59*9f988b79SJean-Baptiste Boric /*
60*9f988b79SJean-Baptiste Boric  * Convert a Unix time_t (non-leap seconds since 1970-01-01) to a RISC
61*9f988b79SJean-Baptiste Boric  * OS time (non-leap(?) centiseconds since 1900-01-01(?)).
62*9f988b79SJean-Baptiste Boric  */
63*9f988b79SJean-Baptiste Boric 
64*9f988b79SJean-Baptiste Boric static u_int64_t
riscos_date(time_t unixtime)65*9f988b79SJean-Baptiste Boric riscos_date(time_t unixtime)
66*9f988b79SJean-Baptiste Boric {
67*9f988b79SJean-Baptiste Boric 	u_int64_t base;
68*9f988b79SJean-Baptiste Boric 
69*9f988b79SJean-Baptiste Boric 	base = 31536000ULL * 70 + 86400 * 17;
70*9f988b79SJean-Baptiste Boric 	return (((u_int64_t)unixtime) + base)*100;
71*9f988b79SJean-Baptiste Boric }
72*9f988b79SJean-Baptiste Boric 
73*9f988b79SJean-Baptiste Boric /*
74*9f988b79SJean-Baptiste Boric  * Add "ARCHIMEDES" metadata to a node if that seems appropriate.
75*9f988b79SJean-Baptiste Boric  *
76*9f988b79SJean-Baptiste Boric  * We touch regular files with names matching /,[0-9a-f]{3}$/ and
77*9f988b79SJean-Baptiste Boric  * directories matching /^!/.
78*9f988b79SJean-Baptiste Boric  */
79*9f988b79SJean-Baptiste Boric static void
archimedes_convert_node(cd9660node * node)80*9f988b79SJean-Baptiste Boric archimedes_convert_node(cd9660node *node)
81*9f988b79SJean-Baptiste Boric {
82*9f988b79SJean-Baptiste Boric 	struct ISO_ARCHIMEDES *arc;
83*9f988b79SJean-Baptiste Boric 	size_t len;
84*9f988b79SJean-Baptiste Boric 	int type = -1;
85*9f988b79SJean-Baptiste Boric 	uint64_t stamp;
86*9f988b79SJean-Baptiste Boric 
87*9f988b79SJean-Baptiste Boric 	if (node->su_tail_data != NULL)
88*9f988b79SJean-Baptiste Boric 		/* Something else already has the tail. */
89*9f988b79SJean-Baptiste Boric 		return;
90*9f988b79SJean-Baptiste Boric 
91*9f988b79SJean-Baptiste Boric 	len = strlen(node->node->name);
92*9f988b79SJean-Baptiste Boric 	if (len < 1) return;
93*9f988b79SJean-Baptiste Boric 
94*9f988b79SJean-Baptiste Boric 	if (len >= 4 && node->node->name[len-4] == ',')
95*9f988b79SJean-Baptiste Boric 		/* XXX should support ,xxx and ,lxa */
96*9f988b79SJean-Baptiste Boric 		type = strtoul(node->node->name + len - 3, NULL, 16);
97*9f988b79SJean-Baptiste Boric 	if (type == -1 && node->node->name[0] != '!')
98*9f988b79SJean-Baptiste Boric 		return;
99*9f988b79SJean-Baptiste Boric 	if (type == -1) type = 0;
100*9f988b79SJean-Baptiste Boric 
101*9f988b79SJean-Baptiste Boric 	assert(sizeof(*arc) == 32);
102*9f988b79SJean-Baptiste Boric 	arc = ecalloc(1, sizeof(*arc));
103*9f988b79SJean-Baptiste Boric 
104*9f988b79SJean-Baptiste Boric 	stamp = riscos_date(node->node->inode->st.st_mtime);
105*9f988b79SJean-Baptiste Boric 
106*9f988b79SJean-Baptiste Boric 	memcpy(arc->magic, "ARCHIMEDES", 10);
107*9f988b79SJean-Baptiste Boric 	cd9660_731(0xfff00000 | (type << 8) | (stamp >> 32), arc->loadaddr);
108*9f988b79SJean-Baptiste Boric 	cd9660_731(stamp & 0x00ffffffffULL, arc->execaddr);
109*9f988b79SJean-Baptiste Boric 	arc->ro_attr = RO_ACCESS_UR | RO_ACCESS_OR;
110*9f988b79SJean-Baptiste Boric 	arc->cdfs_attr = node->node->name[0] == '!' ? CDFS_PLING : 0;
111*9f988b79SJean-Baptiste Boric 	node->su_tail_data = (void *)arc;
112*9f988b79SJean-Baptiste Boric 	node->su_tail_size = sizeof(*arc);
113*9f988b79SJean-Baptiste Boric }
114*9f988b79SJean-Baptiste Boric 
115*9f988b79SJean-Baptiste Boric /*
116*9f988b79SJean-Baptiste Boric  * Add "ARCHIMEDES" metadata to an entire tree recursively.
117*9f988b79SJean-Baptiste Boric  */
118*9f988b79SJean-Baptiste Boric void
archimedes_convert_tree(cd9660node * node)119*9f988b79SJean-Baptiste Boric archimedes_convert_tree(cd9660node *node)
120*9f988b79SJean-Baptiste Boric {
121*9f988b79SJean-Baptiste Boric 	cd9660node *cn;
122*9f988b79SJean-Baptiste Boric 
123*9f988b79SJean-Baptiste Boric 	assert(node != NULL);
124*9f988b79SJean-Baptiste Boric 
125*9f988b79SJean-Baptiste Boric 	archimedes_convert_node(node);
126*9f988b79SJean-Baptiste Boric 
127*9f988b79SJean-Baptiste Boric 		/* Recurse on children. */
128*9f988b79SJean-Baptiste Boric 	TAILQ_FOREACH(cn, &node->cn_children, cn_next_child)
129*9f988b79SJean-Baptiste Boric 		archimedes_convert_tree(cn);
130*9f988b79SJean-Baptiste Boric }
131