summaryrefslogtreecommitdiff
path: root/usr/src/psm/stand/bootblks/hsfs/common/hsfs.fth
blob: 6d855f247a394ab063a5b0fd843ee234ca85c315 (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
\
\ CDDL HEADER START
\
\ The contents of this file are subject to the terms of the
\ Common Development and Distribution License (the "License").
\ You may not use this file except in compliance with the License.
\
\ You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
\ or http://www.opensolaris.org/os/licensing.
\ See the License for the specific language governing permissions
\ and limitations under the License.
\
\ When distributing Covered Code, include this CDDL HEADER in each
\ file and include the License file at usr/src/OPENSOLARIS.LICENSE.
\ If applicable, add the following below this CDDL HEADER, with the
\ fields enclosed by brackets "[]" replaced with your own identifying
\ information: Portions Copyright [yyyy] [name of copyright owner]
\
\ CDDL HEADER END
\
\
\
\ Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
\ Use is subject to license terms.
\

purpose: HSFS file system support package for NewBoot
copyright: Copyright 2009 Sun Microsystems, Inc. All Rights Reserved

\ High Sierra, Rock Ridge (CD-ROM) file system reader and boot block

headers
" /packages" get-package  push-package

new-device
   fs-pkg$  device-name  diag-cr?

   \
   \	HSFS variables
   \
   0 instance value dev-ih
   0 instance value vol-desc
   0 instance value dir-buf
   0 instance value sua-buf
   0 instance value ce-buf

   \
   \	HSFS volume descriptor routines
   \

   \ unaligned load of 2-byte item
   : xw@  ( adr -- n )
      dup c@ swap char+   ( c0 adr+1 )
      c@                  ( c0 c1 )
      bwjoin
   ;

   \ unaligned store of 2-byte item
   : xw!  ( n adr -- )
      swap wbsplit swap 2 pick c! swap char+ c!
   ;

   \ unaligned load of 4-byte item
   : xl@  ( adr -- n )
      dup xw@ swap wa1+   ( w0 adr+2 )
      xw@                 ( w0 w1 )
      wljoin
   ;
   \ unaligned store of 4-byte item
   : xl!  ( n adr -- )
      swap lwsplit swap 2 pick xw! swap wa1+ xw!
   ;

   d# 2048 constant /sector
   d# 16 constant vol-desc-sector#  ( -- n )

   : +vd  ( index -- adr )
      vol-desc 0= if
         ." invalid access of +vd" cr abort
      then
      vol-desc +
   ;

   : root-dir  ( -- n )  d# 156 +vd  ;
   : /block    ( -- n )  d# 128 +vd xw@  ;
   : byte>blkoff  ( byte-off -- block-off )  /block mod  ;

   : get-vol-desc  ( -- )
      vol-desc  /sector  vol-desc-sector# /sector *  dev-ih  read-disk
   ;

   : read-fs-blocks  ( adr len fs-blk# -- )  /block *  dev-ih  read-disk  ;

   \
   \	HSFS directory routines
   \

   \ Current directory variables.
   instance variable cdir-blk		\ Current directory device block ptr.
   instance variable cdir-blk0          \ Current directory block0.
   instance variable cdir-offset	\ Current directory logical offset.
   instance variable cdir-size		\ Current directory logical size.
   instance variable cdir-ptr		\ Current directory entry pointer.
   false instance value cdir-rescan     \ Rescan current directory for symlink.

   \ Access of current directory entry.
   : +dr  ( n -- adr )  cdir-ptr @ +  ;

   : dir-entrylen    ( -- n )    d# 0  +dr c@  ;
   : dir-block0      ( -- n )    d# 2  +dr xl@  ;
   : dir-filesize    ( -- n )    d# 10 +dr xl@  ;
   : dir-flags       ( -- n )    d# 25 +dr c@  ;
   : dir-filenamelen ( -- n )    d# 32 +dr c@  ;
   : dir-filename    ( -- adr )  d# 33 +dr  ;

   : dir-isdir?      ( -- flag )  dir-flags  h# 02  and  0<>  ;
   : dir-file$       ( -- adr len )  dir-filename  dir-filenamelen  ;
   : dir-sualen      ( -- len )  dir-entrylen  d# 33 -  dir-filenamelen -  ;

   \ ISO name, including dot & dot-dot check
   : dir-iso$        ( -- adr len )
      dir-filenamelen 1  =  if
         dir-filename c@             ( name[0] )
         dup 0=  if
            drop " ."  exit          ( dot )
         then
         1 =  if                     (  )
            " .."  exit              ( dot-dot )
         then
      then
      dir-file$                      ( name$ )
   ;

   false instance value symlink?

   : get-dirblk  ( -- )
      dir-buf /block  cdir-blk @  read-fs-blocks
      1 cdir-blk +!
   ;

   : froot  ( -- )  root-dir cdir-ptr !  ;

   \
   \ SUAs - System Use Area in directory entry (Rock Ridge
   \  Extensions to High Sierra/ISO 9660 Format).
   \  Immediately follows directory entry name rounded up to
   \  a half-word boundary.
   \
   0 instance value sua-ptr
   0 instance value sua-len

   : +suf           ( n -- adr )    sua-ptr +  ;
   : suf-sig        ( -- adr len )  sua-ptr 2  ;
   : suf-len        ( -- len )      2 +suf c@  ;
   : suf-dat        ( -- data )     5 +suf  ;
   : suf-ce-lbn     ( -- lbn )      4 +suf xl@ ;
   : suf-ce-offset  ( -- offset )   d# 12 +suf xl@ ;
   : suf-ce-len     ( -- len )      d# 20 +suf xl@ ;

   : init-sua     ( -- )
      dir-file$ +  /w roundup  to sua-ptr
      dir-sualen               to sua-len
   ;

   : next-suf  ( -- )
      sua-len suf-len -  to sua-len
      suf-len +suf       to sua-ptr
   ;

   : end-sua  ( -- end? )
      sua-len 4 <
   ;

   : suf-nm$  ( -- adr len )  suf-dat  suf-len 5 -  ;

   \ Continuation suffix handling.  When a 'CE' suffix is seen,
   \ record the CE parameters (logical block#, offset and length
   \ of continuation).  We process the CE continuation only after
   \ we've finished processing the current SUA area.
   instance variable ce-lbn
   instance variable ce-offset
   instance variable ce-len
   : suf-ce-set  ( -- )
      suf-ce-lbn ce-lbn !
      suf-ce-offset ce-offset !
      suf-ce-len ce-len !
   ;
      
   : suf-ce-process  ( -- error? )
      ce-lbn @ 0= if
         true
      else
         sua-buf ce-len @ ce-lbn @  read-fs-blocks
         sua-buf   to sua-ptr
         ce-len @  to sua-len
         0 ce-len ! 0 ce-lbn ! 0 ce-offset !
         false
      then
   ;

   /buf-len  instance buffer: suf-sl-buf
   false     instance value   symlink-need-sep

   \ Format of Rock Ridge symlinks needs to be munged to unix-style
   \ name.  Format is:  <flag><nbytes>file-name<flag><nbytes>filename...
   \ where \ <flag> is flag byte (0=filename, 2=current dir, 4=parent
   \ dir, 8=root dir) and <nbytes> is one-byte byte count (zero for
   \ !filename).
   : suf-copy-to-symlinkbuf  ( name$  -- )
       false to symlink-need-sep
       suf-sl-buf -rot bounds do           ( dst )
          symlink-need-sep if
             ascii / over c! char+
          then
          true to symlink-need-sep
          i c@ dup 2 = if                   ( dst 2 )
             \ CURRENT (".")
             drop ascii . over c! char+ 2   ( dst' inc )
          else  dup 4 =  if                 ( dst 4 )
             \ PARENT ("..")
             drop " .." 2 pick swap move    ( dst )
             wa1+ 2                         ( dst' inc )
          else  dup 8 =  if                 ( dst 8 )
             \ ROOT ("/")
             drop ascii / over c! char+ 2   ( dst' inc )
             false to symlink-need-sep
          else  dup 0<> if
             ." unknown SL flag: " .x cr abort
          else                              ( dst c )
             drop                           ( dst )
             i char+ dup c@ >r              ( dst src+1  R:nbytes )
             char+ over r@ move             ( dst R:nbytes )
             r@ +                           ( dst' R:nbytes )
             r> wa1+                        ( dst' inc )
          then then then then
       +loop                                ( dst )
       0 swap c!
    ; 

   \ Saved 'NM' prefix buffer.
   /buf-len  instance buffer: suf-nm-buf
   0 instance value suf-nm-size
      
   \ Return the Rock Ridge file name associated with the current
   \ dirent ('NM' suffix).  Otherwise returns standard iso filename.
   \ Marks whether returned filename is a symbolic link ('SL' suffix)
   \ and also processes continuations ('CE' suffix).
   : rr-file$ ( -- adr len )
      false to symlink?
      0 to suf-nm-size

      \ select start of sua, record sua offset
      init-sua
      begin
         end-sua  if
            suf-ce-process if
               suf-nm-size if
                  suf-nm-buf suf-nm-size       ( NM$ )
               else
                  dir-iso$                     ( iso$ )
               then                            ( file$ )
               exit
            then
         then
         suf-sig                               ( sig-adr sig-len )
         2dup " NM"  $=  if
            suf-nm$ to suf-nm-size             ( sig-adr sig-len suf-nm-adr )
            suf-nm-buf suf-nm-size move
         then                                  ( sig-adr sig-len )
         2dup " SL"  $=  if
            true to symlink?
            suf-nm$ suf-copy-to-symlinkbuf
         then
         2dup " CE"  $=  if
            suf-ce-set
         then                                  ( sig-adr sig-len )
         2drop  next-suf                       (  )
      again
   ;

   \
   \	HSFS high-level routines
   \

   \ Used for rescanning current directory for symbolic links.

   \ Initializes current directory settings from current directory
   \ entry pointer or for rescan.  If it's not a rescan, we have
   \ access to the actual directory entry, so we can check whether
   \ it's a directory or not here.
   : init-dent  ( -- error? )
      cdir-rescan if
         false to cdir-rescan
         cdir-blk0 @ cdir-blk !
      else
         dir-isdir? 0= if
            true exit
         then
         dir-block0 dup cdir-blk ! cdir-blk0 !
         dir-filesize cdir-size !
      then                                    ( blk0 size )
      0 cdir-offset !
      false
   ;

   : get-dent ( -- error? )
      begin
         \ Check for end of directory, return true if we're past the EOF.
         cdir-offset @  cdir-size @  >=  if
            true  exit
         then

         \ If we're at a block boundary, get the next block.  Otherwise
         \ increment the directory pointer.
         cdir-offset @ byte>blkoff  0=  if
            get-dirblk
            dir-buf cdir-ptr !
         else
            dir-entrylen cdir-ptr +!
         then

         \ If dir-entrylen is not zero, increment the current directory
         \ file offset.  Otherwise, a dir-entrylen of zero indicates
         \ the end of a dir block, so round up cdir-offset to fetch the
         \ next one
         dir-entrylen ?dup if
            cdir-offset +!  true
         else
            cdir-offset @  /block  roundup  cdir-offset !
            false
         then
      until  false
   ;

   \ Look through current directory for file name 'file$'.
   \ Will leave current directory entry (cdir-ptr) pointing
   \ to matched entry on success.
   : dirlook  ( file$ -- error? )
      init-dent if
         true exit
      then
      begin  get-dent 0=  while      ( file$ )
         2dup rr-file$ $=  if        ( file$ )
            2drop false  exit        ( succeeded )
         then                        ( file$ )
      repeat 2drop true              ( failed )
   ;

   /buf-len  instance buffer: symlink-buf
   : symlink-buf$  ( -- path$ )  symlink-buf cscount  ;

   : follow-symlink  ( tail$ -- tail$' )

      \ copy symlink value (plus null) to buf
      suf-sl-buf cscount 1+  symlink-buf swap  move
      false to symlink?

      \ append to current path
      ?dup  if                                              ( tail$ )
	 " /" symlink-buf$  $append                         ( tail$ )
	 symlink-buf$  $append                              (  )
      else  drop  then                                      (  )
      symlink-buf$                                          ( path$ )
      over c@  ascii /  =  if                               ( path$ )
	 froot  str++                                       ( path$' )
      else
         true to cdir-rescan
      then                                                  ( path$ )
   ;

   : lookup  ( path$ -- error? )
      over c@  ascii /  =  if
	 froot  str++                            ( path$' )
      then                                       ( path$ )
      begin                                      ( path$ )
         ascii / left-parse-string               ( path$ file$ )
      dup  while                                 ( path$ file$ )
         dirlook  if
            2drop true  exit                     ( failed )
         then                                    ( path$ )
         symlink?  if
            follow-symlink                       ( path$' )
         then                                    ( path$ )
      repeat                                     ( path$ file$ )
      2drop 2drop  false                         ( succeeded )
   ;


   \
   \	HSFS installation routines
   \

   \ Allocate memory for necessary data structures.  Need to
   \ read volume desriptor sector in order to get /block value.
   : initialize  ( -- error? )
      /sector  mem-alloc to vol-desc
      get-vol-desc
      /block   mem-alloc to dir-buf
      /block   mem-alloc to sua-buf
      /block   mem-alloc to ce-buf
   ;

   : release-buffers  ( -- )
      ce-buf      /block  mem-free
      sua-buf	  /block  mem-free
      dir-buf     /block  mem-free
      vol-desc    /sector mem-free
      0 to vol-desc
   ;


   \ HSFS file interface
   struct
      /x     field >filesize
      /x     field >offset
      /x     field >block0
   constant /file-record

   d# 10                  constant #opens
   #opens /file-record *  constant /file-records

   /file-records  instance buffer: file-records

   -1 instance value current-fd

   : fd>record  ( fd -- record )  /file-record *  file-records +  ;

   : set-fd  ( fd -- error? )
      dup 0 #opens 1 - between 0= if
         drop true exit
      then
      dup fd>record  >block0 x@ 0= if
         drop true exit
      then
      to current-fd false
   ;

   : file-offset@  ( -- off )
      current-fd fd>record >offset x@
   ;

   : file-offset!  ( off -- )
      current-fd fd>record >offset x!
   ;

   : file-size@  ( -- size )
      current-fd fd>record >filesize x@
   ;

   : file-size!  ( size -- )
      current-fd fd>record >filesize x!
   ;

   : file-block0@  ( -- block0 )
      current-fd fd>record >block0 x@
   ;

   : file-block0!  ( block0 -- )
      current-fd fd>record >block0 x!
   ;

   : get-slot  ( -- fd false | true )
      #opens 0  do
         i fd>record >block0 x@  0=  if
            i false  unloop exit
         then
      loop  true
   ;

   : free-slot  ( fd -- )
      set-fd 0= if
         0 file-offset!
         0 file-size!
         0 file-block0!
      then
   ;

   \ initializes the open structure with information from
   \ the inode (on UFS) or directory entry (from HSFS).
   : init-fd  ( fd -- )
      to current-fd
      dir-block0 file-block0!
      dir-filesize file-size!
      0 file-offset!
   ;

   external

   : open ( -- okay? )
      my-args dev-open  dup 0=  if       ( 0 )
         exit                            ( failed )
      then  to dev-ih

      initialize  froot
      file-records /file-records  erase
      true                               ( succeeded )
   ;

   : close  ( -- )
      dev-ih dev-close
      release-buffers
   ;

   : open-file  ( path$ -- fd true | false )
      get-slot  if
	 2drop false  exit            ( failed )
      then  -rot                      ( fd path$ )

      lookup  if                      ( fd )
	 drop false  exit             ( failed )
      then

      dup init-fd true                ( fd success )
   ;

   : close-file  ( fd -- )
      free-slot   (  )
   ;

   : read-file   ( adr len fd -- #read )

      \ Check if fd is valid, if it is set current-fd.
      set-fd if
         2drop 0 exit
      then                                   ( adr len )

      \ Adjust len if less than len bytes remain.
      file-size@ file-offset@ - min          ( adr len' )

      \ Check for invalid length read.
      dup 0<=  if  2drop 0 exit  then

      \ Compute physical device byte offset.
      tuck                                   ( len adr len )
      file-block0@ /block * file-offset@ +   ( len adr len off )
      dev-ih read-disk                       ( #read )
      dup file-offset@ +  file-offset!
   ;

   : seek-file  ( off fd -- error? )
      set-fd  if                ( off )
         drop false  exit       ( failed )
      then                      ( off )

      dup file-size@ >  if      ( off )
         drop false  exit       ( failed )
      then                      ( off )
      dup  file-offset!  true   ( off succeeded )
   ;

   : size-file  ( fd -- size )
      set-fd if
         0
      else
         file-size@
      then
   ;

   \ we don't support compression (yet)
   : cinfo-file  ( fd -- bsize fsize comp? )
      set-fd  if  0 0 0  else  /block file-size@ 0  then
   ;

   \ read ramdisk fcode at rd-offset
   : get-rd   ( adr len -- )
      rd-offset dev-ih  read-disk
   ;

   \ no additional props needed for hsfs
   : bootprop  ( -- )  false  ;

   \ debug words
   : chdir  ( path$ -- )
      2dup lookup if
         type ."  Not found" cr
      else
         dir-isdir? 0= if
            type ."  Not a directory" cr
         else
            type
	    ."  blk0 "
            cdir-blk0 @ .x
            ."  size "
            cdir-size @ .x
            cr
         then
      then
   ;

   : dir  ( -- )
      init-dent
      begin  get-dent 0=  while
         rr-file$ type
         ."  flags " dir-flags .x
         ." blk0 " dir-block0 .x
         ." size " dir-filesize .x
         cr
      repeat
      true to cdir-rescan
   ;
      

finish-device
pop-package