]> git.ozlabs.org Git - yaboot.git/blobdiff - second/yaboot.c
Pretend to allocate/deallocate memory correctly
[yaboot.git] / second / yaboot.c
index a061275dab85b4e42bedb7d45c3767b302e2bf30..cf920a1646f2f5ae4290c64120ba5c7934b33871 100644 (file)
@@ -1,38 +1,45 @@
-/* Yaboot - secondary boot loader for Linux on ppc.
-
-   Copyright (C) 1999 Benjamin Herrenschmidt
-
-   portions based on poof
-
-   Copyright (C) 1999 Marius Vollmer
-
-   portions based on quik
-   
-   Copyright (C) 1996 Paul Mackerras.
-
-   Because this program is derived from the corresponding file in the
-   silo-0.64 distribution, it is also
-
-   Copyright (C) 1996 Pete A. Zaitcev
-                1996 Maurizio Plaza
-                1996 David S. Miller
-                1996 Miguel de Icaza
-                1996 Jakub Jelinek
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
-   (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-*/
+/*
+ *  Yaboot - secondary boot loader for Linux on PowerPC. 
+ *
+ *  Copyright (C) 2001, 2002 Ethan Benson
+ *
+ *  Copyright (C) 1999, 2000, 2001 Benjamin Herrenschmidt
+ *  
+ *  IBM CHRP support
+ *  
+ *  Copyright (C) 2001 Peter Bergner
+ *
+ *  portions based on poof
+ *  
+ *  Copyright (C) 1999 Marius Vollmer
+ *  
+ *  portions based on quik
+ *  
+ *  Copyright (C) 1996 Paul Mackerras.
+ *  
+ *  Because this program is derived from the corresponding file in the
+ *  silo-0.64 distribution, it is also
+ *  
+ *  Copyright (C) 1996 Pete A. Zaitcev
+ *                1996 Maurizio Plaza
+ *                1996 David S. Miller
+ *                1996 Miguel de Icaza
+ *                1996 Jakub Jelinek
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ */
 
 #include "stdarg.h"
 #include "string.h"
@@ -428,7 +435,7 @@ bail:
      if (opened)
          file.fs->close(&file);
     
-     if (result != 1 && conf_file)
+     if (conf_file)
          free(conf_file);
        
      return result;
@@ -1074,7 +1081,7 @@ load_elf32(struct boot_file_t *file, loadinfo_t *loadinfo)
      /* Read the rest of the Elf header... */
      if ((*(file->fs->read))(file, size, &e->e_version) < size) {
          prom_printf("\nCan't read Elf32 image header\n");
-         return 0;
+         goto bail;
      }
 
      DEBUG_F("Elf32 header:\n");
@@ -1093,24 +1100,24 @@ load_elf32(struct boot_file_t *file, loadinfo_t *loadinfo)
 
      if (e->e_phnum > MAX_HEADERS) {
          prom_printf ("Can only load kernels with one program header\n");
-         return 0;
+         goto bail;
      }
 
      ph = (Elf32_Phdr *)malloc(sizeof(Elf32_Phdr) * e->e_phnum);
      if (!ph) {
          prom_printf ("Malloc error\n");
-         return 0;
+         goto bail;
      }
 
      /* Now, we read the section header */
      if ((*(file->fs->seek))(file, e->e_phoff) != FILE_ERR_OK) {
          prom_printf ("seek error\n");
-         return 0;
+         goto bail;
      }
      if ((*(file->fs->read))(file, sizeof(Elf32_Phdr) * e->e_phnum, ph) !=
         sizeof(Elf32_Phdr) * e->e_phnum) {
          prom_printf ("read error\n");
-         return 0;
+         goto bail;
      }
 
      /* Scan through the program header
@@ -1137,7 +1144,7 @@ load_elf32(struct boot_file_t *file, loadinfo_t *loadinfo)
 
      if (loadinfo->memsize == 0) {
          prom_printf("Can't find a loadable segment !\n");
-         return 0;
+         goto bail;
      }
 
      /* leave some room (1Mb) for boot infos */
@@ -1166,7 +1173,7 @@ load_elf32(struct boot_file_t *file, loadinfo_t *loadinfo)
      }
      if (loadinfo->base == (void *)-1) {
          prom_printf("Claim error, can't allocate kernel memory\n");
-         return 0;
+         goto bail;
      } 
 
      DEBUG_F("After ELF parsing, load base: %p, mem_sz: 0x%08lx\n",
@@ -1185,13 +1192,13 @@ load_elf32(struct boot_file_t *file, loadinfo_t *loadinfo)
          if ((*(file->fs->seek))(file, p->p_offset) != FILE_ERR_OK) {
               prom_printf ("Seek error\n");
               prom_release(loadinfo->base, loadinfo->memsize);
-              return 0;
+              goto bail;
          }
          offset = p->p_vaddr - loadinfo->load_loc;
          if ((*(file->fs->read))(file, p->p_filesz, loadinfo->base+offset) != p->p_filesz) {
               prom_printf ("Read failed\n");
               prom_release(loadinfo->base, loadinfo->memsize);
-              return 0;
+              goto bail;
          }
      }
 
@@ -1199,6 +1206,11 @@ load_elf32(struct boot_file_t *file, loadinfo_t *loadinfo)
     
      /* Return success at loading the Elf32 kernel */
      return 1;
+
+bail:
+     if (ph)
+       free(ph);
+     return 0;
 }
 
 static int
@@ -1213,7 +1225,7 @@ load_elf64(struct boot_file_t *file, loadinfo_t *loadinfo)
      /* Read the rest of the Elf header... */
      if ((*(file->fs->read))(file, size, &e->e_version) < size) {
          prom_printf("\nCan't read Elf64 image header\n");
-         return 0;
+         goto bail;
      }
 
      DEBUG_F("Elf64 header:\n");
@@ -1232,24 +1244,24 @@ load_elf64(struct boot_file_t *file, loadinfo_t *loadinfo)
 
      if (e->e_phnum > MAX_HEADERS) {
          prom_printf ("Can only load kernels with one program header\n");
-         return 0;
+         goto bail;
      }
 
      ph = (Elf64_Phdr *)malloc(sizeof(Elf64_Phdr) * e->e_phnum);
      if (!ph) {
          prom_printf ("Malloc error\n");
-         return 0;
+         goto bail;
      }
 
      /* Now, we read the section header */
      if ((*(file->fs->seek))(file, e->e_phoff) != FILE_ERR_OK) {
          prom_printf ("Seek error\n");
-         return 0;
+         goto bail;
      }
      if ((*(file->fs->read))(file, sizeof(Elf64_Phdr) * e->e_phnum, ph) !=
         sizeof(Elf64_Phdr) * e->e_phnum) {
          prom_printf ("Read error\n");
-         return 0;
+         goto bail;
      }
 
      /* Scan through the program header
@@ -1276,7 +1288,7 @@ load_elf64(struct boot_file_t *file, loadinfo_t *loadinfo)
 
      if (loadinfo->memsize == 0) {
          prom_printf("Can't find a loadable segment !\n");
-         return 0;
+         goto bail;
      }
 
      /* leave some room (1Mb) for boot infos */
@@ -1305,7 +1317,7 @@ load_elf64(struct boot_file_t *file, loadinfo_t *loadinfo)
      }
      if (loadinfo->base == (void *)-1) {
          prom_printf("Claim error, can't allocate kernel memory\n");
-         return 0;
+         goto bail;
      } 
 
      DEBUG_F("After ELF parsing, load base: %p, mem_sz: 0x%08lx\n",
@@ -1324,13 +1336,13 @@ load_elf64(struct boot_file_t *file, loadinfo_t *loadinfo)
          if ((*(file->fs->seek))(file, p->p_offset) != FILE_ERR_OK) {
               prom_printf ("Seek error\n");
               prom_release(loadinfo->base, loadinfo->memsize);
-              return 0;
+              goto bail;
          }
          offset = p->p_vaddr - loadinfo->load_loc;
          if ((*(file->fs->read))(file, p->p_filesz, loadinfo->base+offset) != p->p_filesz) {
               prom_printf ("Read failed\n");
               prom_release(loadinfo->base, loadinfo->memsize);
-              return 0;
+              goto bail;
          }
      }
 
@@ -1338,6 +1350,11 @@ load_elf64(struct boot_file_t *file, loadinfo_t *loadinfo)
     
      /* Return success at loading the Elf64 kernel */
      return 1;
+
+bail:
+     if (ph)
+       free(ph);
+     return 0;
 }
 
 static int
@@ -1514,11 +1531,13 @@ yaboot_main(void)
               if ((ptype != NULL) && (strcmp(ptype, "Apple_Bootstrap")))
                    prom_printf("\nWARNING: Bootstrap partition type is wrong: \"%s\"\n"
                                "         type should be: \"Apple_Bootstrap\"\n\n", ptype);
+              if (ptype)
+                   free(ptype);
          }
      }
 
      yaboot_text_ui();
-       
+
      prom_printf("Bye.\n");
      return 0;
 }