fs.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * (C) Copyright 2002
  3. * Stäubli Faverges - <www.staubli.com>
  4. * Pierre AUBERT p.aubert@staubli.com
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <config.h>
  26. #include <malloc.h>
  27. #if (CONFIG_COMMANDS & CFG_CMD_FDOS)
  28. #include "dos.h"
  29. #include "fdos.h"
  30. /*-----------------------------------------------------------------------------
  31. * fill_fs -- Read info on file system
  32. *-----------------------------------------------------------------------------
  33. */
  34. static int fill_fs (BootSector_t *boot, Fs_t *fs)
  35. {
  36. fs -> fat_start = __le16_to_cpu (boot -> nrsvsect);
  37. fs -> fat_len = __le16_to_cpu (boot -> fatlen);
  38. fs -> nb_fat = boot -> nfat;
  39. fs -> dir_start = fs -> fat_start + fs -> nb_fat * fs -> fat_len;
  40. fs -> dir_len = __le16_to_cpu (boot -> dirents) * MDIR_SIZE / SZ_STD_SECTOR;
  41. fs -> cluster_size = boot -> clsiz;
  42. fs -> num_clus = (fs -> tot_sectors - fs -> dir_start - fs -> dir_len) / fs -> cluster_size;
  43. return (0);
  44. }
  45. /*-----------------------------------------------------------------------------
  46. * fs_init --
  47. *-----------------------------------------------------------------------------
  48. */
  49. int fs_init (Fs_t *fs)
  50. {
  51. BootSector_t *boot;
  52. /* Initialize physical device */
  53. if (dev_open () < 0) {
  54. PRINTF ("Unable to initialize the fdc\n");
  55. return (-1);
  56. }
  57. init_subdir ();
  58. /* Allocate space for read the boot sector */
  59. if ((boot = (BootSector_t *)malloc (sizeof (BootSector_t))) == NULL) {
  60. PRINTF ("Unable to allocate space for boot sector\n");
  61. return (-1);
  62. }
  63. /* read boot sector */
  64. if (dev_read (boot, 0, 1)){
  65. PRINTF ("Error during boot sector read\n");
  66. free (boot);
  67. return (-1);
  68. }
  69. /* we verify it'a a DOS diskette */
  70. if (boot -> jump [0] != JUMP_0_1 && boot -> jump [0] != JUMP_0_2) {
  71. PRINTF ("Not a DOS diskette\n");
  72. free (boot);
  73. return (-1);
  74. }
  75. if (boot -> descr < MEDIA_STD) {
  76. /* We handle only recent medias (type F0) */
  77. PRINTF ("unrecognized diskette type\n");
  78. free (boot);
  79. return (-1);
  80. }
  81. if (check_dev (boot, fs) < 0) {
  82. PRINTF ("Bad diskette\n");
  83. free (boot);
  84. return (-1);
  85. }
  86. if (fill_fs (boot, fs) < 0) {
  87. free (boot);
  88. return (-1);
  89. }
  90. /* Read FAT */
  91. if (read_fat (boot, fs) < 0) {
  92. free (boot);
  93. return (-1);
  94. }
  95. free (boot);
  96. return (0);
  97. }
  98. #endif