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